home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume26 / arc-5.21 / part02 < prev    next >
Encoding:
Text File  |  1992-04-10  |  88.1 KB  |  3,343 lines

  1. Newsgroups: comp.sources.unix
  2. From: hyc@umix.cc.umich.edu (Howard Chu)
  3. Subject: v26i005: arc 5.21 (pl 8) - file archive/compression tool, Part02/03
  4. Sender: unix-sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: hyc@umix.cc.umich.edu (Howard Chu)
  8. Posting-Number: Volume 26, Issue 5
  9. Archive-Name: arc-5.21/part02
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 2 (of 3)."
  18. # Contents:  arc.1 arc.c arcadd.c arclzw.c arcmisc.c arcsq.c marc.c
  19. # Wrapped by vixie@cognition.pa.dec.com on Sat Apr 11 13:54:35 1992
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'arc.1' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'arc.1'\"
  23. else
  24. echo shar: Extracting \"'arc.1'\" \(7206 characters\)
  25. sed "s/^X//" >'arc.1' <<'END_OF_FILE'
  26. X.TH ARC 1L "11 Nov 1991" "Howard Chu@JPL" "LOCAL COMMANDS"
  27. X.SH NAME
  28. arc \- pc archive utility
  29. X.SH SYNOPSIS
  30. X.B arc
  31. a|m|u|f|d|x|e|r|p|l|v|t|c [ biswnoq ] [ g\fIpassword\fR ]
  32. X.I archive 
  33. X[ \fIfilename\fR ...]
  34. X.SH DESCRIPTION
  35. X.I Arc
  36. is a general archive and file compression utility, used to maintain
  37. a compressed archive of files.
  38. An
  39. X.I archive
  40. is a single file that combines many files, reducing storage space
  41. and allowing multiple files to be handled as one.
  42. X.I Arc
  43. uses one of several compression methods for each file within the
  44. X.IR archive ,
  45. based on whichever method yields the smallest result.
  46. X.SH INSTRUCTIONS
  47. XExecute
  48. X.I arc
  49. with no arguments for fairly verbose, usable instructions.
  50. X.SH COMMAND SWITCHES
  51. X.TP 3
  52. a
  53. add files to archive.  Copies the indicated files to the archive.
  54. X.TP
  55. m
  56. move files to archive.  Same as 'a' switch except
  57. that the files are deleted from the directory as
  58. they are moved to the archive.
  59. X.TP
  60. u
  61. update files in archive.  This switch will replace archived
  62. files when the named file is newer than the archived copy.
  63. New files will be added automatically.
  64. X.TP
  65. f
  66. freshen files in archive.  Same as 'u' except that
  67. new files will not be added.
  68. X.TP
  69. d
  70. delete files in archive.  The named files are removed from the archive.
  71. X.TP
  72. x,e
  73. extract files from archive.  The named files are extracted
  74. from the archive and created in the current directory
  75. in an uncompressed state.
  76. X.TP
  77. r
  78. run one file with arguments from archive.  Any
  79. program may be executed directly from the archive.
  80. The parameters given after the program name are passed to
  81. the program without modification.
  82. X.TP
  83. p
  84. copy files from archive to standard output.  Useful
  85. with I/O redirection. A form-feed is appended after each file,
  86. to ease use with printers.
  87. X.TP
  88. l
  89. list files in archive.  Limited information listing
  90. of files contained in an archive.  Displays the
  91. filename, original length, and date last modified.
  92. If the 'n' option (see below) is used, only the
  93. filename is displayed.
  94. X.TP
  95. v
  96. verbose listing of files in archive.  Complete
  97. information listing of files contained in an archive.
  98. Displays the filename, original length, storage method,
  99. storage factor (% savings), compressed size, date, time,
  100. and CRC.
  101. X.TP
  102. t
  103. test archive integrity.  Computes CRC values for each member of
  104. the archive and compares against the previously saved value.
  105. X.TP
  106. c
  107. convert entry to new packing method.  Convert files
  108. stored with older methods to newer methods that are
  109. more efficient. Also useful for files previously
  110. archived with the 's' option.
  111. X.SH OPTIONS
  112. X.TP 3
  113. b
  114. retain backup copy of archive.  Keep the original
  115. archive file and rename to .BAK.
  116. This switch may be used with the
  117. following commands:  a, m, u, f, d, c.
  118. X.TP
  119. i
  120. suppress image mode.  This switch causes files to
  121. be treated as text files, and will translate their
  122. end-of-line sequence. (Unix's '\\n' vs. '\\r\\n' used
  123. on many other systems.)  The default is to perform
  124. no translation when compressing or extracting files.
  125. This option makes dealing with text files much nicer,
  126. though the 'tr' command can also be used. ('\\r' in
  127. makefiles and C source code is such a nuisance...)
  128. X.TP
  129. s
  130. suppress compression.  This forces new files to be
  131. saved using Method 2 (no compression).  This switch
  132. may be used with the following commands:  a, m, u, f, c.
  133. X.TP
  134. w
  135. suppress warning messages.  This switch will keep
  136. warning messages from being displayed which is the default.
  137. Most warnings concern the deletion or existence of
  138. files with the same name.
  139. X.TP
  140. n
  141. suppress notes and comments.  This switch will keep
  142. useful notes from being displayed which is the default.
  143. Most notes indicate what stage of compression is
  144. being run (analyze, compaction, storage).
  145. X.TP
  146. o
  147. overwrite existing files when extracting.  This switch
  148. will make existing files silently get overwritten, instead
  149. of asking for confirmation, which is the default.
  150. X.TP
  151. q
  152. force Squash compression method.  This switch causes
  153. the Squash compression method to be used, instead of
  154. Crunch, which is the default.
  155. X.TP
  156. g
  157. encrypt/decrypt archive entry.  This is used to encode
  158. files so that others may not read them.  BE CAREFUL!
  159. This must be the last parameter in the switches because
  160. everything following is part of the password.
  161. X.SH PROGRAMMING NOTES
  162. X.I Arc
  163. Version 2 differs from version 1 in that archive entries
  164. are automatically compressed when they are added to the archive,
  165. making a separate compression step unecessary.  The nature of the
  166. compression is indicated by the header version number placed in
  167. each archive entry, as follows:
  168. X.nf
  169. X         1 = Old style, no compression
  170. X         2 = New style, no compression
  171. X         3 = Compression of repeated characters only
  172. X         4 = Compression of repeated characters plus Huffman SQueezing
  173. X         5 = Lempel-Zev packing of repeated strings (old style)
  174. X         6 = Lempel-Zev packing of repeated strings (new style)
  175. X         7 = Lempel-Zev Williams packing with improved hash function
  176. X         8 = Dynamic Lempel-Zev packing with adaptive reset
  177. X         9 = Squashing
  178. X.fi
  179. X
  180. Type 5, Lempel-Zev packing, was added as of version 4.0
  181. X
  182. Type 6 is Lempel-Zev packing where runs of repeated characters
  183. have been collapsed, and was added as of version 4.1
  184. X
  185. Type 7 is a variation of Lempel-Zev using a different hash
  186. function which yields speed improvements of 20-25%, and was
  187. added as of version 4.6
  188. X
  189. Type 8 is a different implementation of Lempel-Zev, using a
  190. variable code size and an adaptive block reset, and was added
  191. as of version 5.0
  192. X
  193. Type 9 is another variation of Lempel-Zev, using a larger
  194. hash table. This method was developed by Phil Katz, and is
  195. not supported by the "official" \fBARC\fP programs.
  196. X
  197. X.I Arc
  198. will look for environment variables named \fIARCTEMP\fP or
  199. X\fITMPDIR\fP, which, if present, indicates the pathname
  200. where temporary files should be created. This is typically
  201. the location of a RAMdisk on a microcomputer, "/tmp/" or
  202. left unset.
  203. X
  204. See the included documentation file for more details.
  205. X.SH HISTORY
  206. X\fIArc\fP has been in use in the CP/M and MSDOS world for many years.
  207. Thom Henderson developed the original version, but it is important to note that
  208. X\fIarc\fP is based on the file compression theories developed by Huffman, Welch,
  209. Knott, Knuth, and many other scientists. This implementation is based on
  210. version 5.21 of the MSDOS program.
  211. X.SH BUGS
  212. X\fIArc\fP behaves just like the PC version of the program; all functions
  213. of the "usage" display are working.
  214. XFull compatibility with PC ARC files is maintained, the price for which is
  215. that \fIarc\fP doesn't like long filenames, and can only archive files with
  216. names of up to 12 characters.
  217. It will *sometimes* do The Right Thing with them, but I suggest
  218. you put long-winded filenames in a "shar" before
  219. X.IR arc ing
  220. them.
  221. X
  222. There shouldn't be any problems, (hah!) but if you find any, please
  223. send them to me at:
  224. X
  225. X    hyc@hanauma.jpl.nasa.gov
  226. X
  227. X.SH AUTHORS
  228. Original MSDOS program by Thom Henderson
  229. X.br
  230. COPYRIGHT(C) 1985-87 by System Enhancement Associates; ALL RIGHTS RESERVED
  231. X
  232. Original Lempel-Zev code derived from compress 4.0.
  233. Modified to support Squashing by Dan Lanciani (ddl@harvard.edu)
  234. Ported from MSDOS by Howard Chu,
  235. with help from John Gilmore (hoptoad!gnu), James Turner (daisy!turner)
  236. and others.
  237. END_OF_FILE
  238. if test 7206 -ne `wc -c <'arc.1'`; then
  239.     echo shar: \"'arc.1'\" unpacked with wrong size!
  240. fi
  241. # end of 'arc.1'
  242. fi
  243. if test -f 'arc.c' -a "${1}" != "-c" ; then 
  244.   echo shar: Will not clobber existing file \"'arc.c'\"
  245. else
  246. echo shar: Extracting \"'arc.c'\" \(11929 characters\)
  247. sed "s/^X//" >'arc.c' <<'END_OF_FILE'
  248. X/*
  249. X * $Header: /var/local/hyc/src/arc/RCS/arc.c,v 2.0 1991/11/12 00:30:01 hyc Exp $
  250. X */
  251. X
  252. X/*  ARC - Archive utility
  253. X  
  254. X    Version 5.21, created on 04/22/87 at 15:05:21
  255. X  
  256. X(C) COPYRIGHT 1985-87 by System Enhancement Associates; ALL RIGHTS RESERVED
  257. X  
  258. X    By:     Thom Henderson
  259. X  
  260. X    Description:
  261. X     This program is a general archive utility, and is used to maintain
  262. X     an archive of files.  An "archive" is a single file that combines
  263. X     many files, reducing storage space and allowing multiple files to
  264. X     be handled as one.
  265. X  
  266. X    Instructions:
  267. X     Run this program with no arguments for complete instructions.
  268. X  
  269. X    Programming notes:
  270. X     ARC Version 2 differs from version 1 in that archive entries
  271. X     are automatically compressed when they are added to the archive,
  272. X     making a separate compression step unecessary.     The nature of the
  273. X     compression is indicated by the header version number placed in
  274. X     each archive entry, as follows:
  275. X  
  276. X     1 = Old style, no compression
  277. X     2 = New style, no compression
  278. X     3 = Compression of repeated characters only
  279. X     4 = Compression of repeated characters plus Huffman SQueezing
  280. X     5 = Lempel-Zev packing of repeated strings (old style)
  281. X     6 = Lempel-Zev packing of repeated strings (new style)
  282. X     7 = Lempel-Zev Williams packing with improved hash function
  283. X     8 = Dynamic Lempel-Zev packing with adaptive reset
  284. X     9 = Dynamic Lempel-Zev packing, larger hash table
  285. X  
  286. X     Type 5, Lempel-Zev packing, was added as of version 4.0
  287. X  
  288. X     Type 6 is Lempel-Zev packing where runs of repeated characters
  289. X     have been collapsed, and was added as of version 4.1
  290. X  
  291. X     Type 7 is a variation of Lempel-Zev using a different hash
  292. X     function which yields speed improvements of 20-25%, and was
  293. X     added as of version 4.6
  294. X  
  295. X     Type 8 is a different implementation of Lempel-Zev, using a
  296. X     variable code size and an adaptive block reset, and was added
  297. X     as of version 5.0
  298. X  
  299. X     Type 9 is a slight modification of type 8, first used by Phil
  300. X     Katz in his PKARC utilites. The primary difference is the use
  301. X     of a hash table twice as large as for type 8, and that this
  302. X     algorithm called Squashing, doesn't perform run-length encoding
  303. X     on the input data.
  304. X  
  305. X     Verion 4.3 introduced a temporary file for holding the result
  306. X     of the first crunch pass, thus speeding up crunching.
  307. X  
  308. X     Version 4.4 introduced the ARCTEMP environment string, so that
  309. X     the temporary crunch file may be placed on a ramdisk.    Also
  310. X     added was the distinction bewteen Adding a file in all cases,
  311. X     and Updating a file only if the disk file is newer than the
  312. X     corresponding archive entry.
  313. X  
  314. X     The compression method to use is determined when the file is
  315. X     added, based on whichever method yields the smallest result.
  316. X  
  317. X    Language:
  318. X     Computer Innovations Optimizing C86
  319. X*/
  320. X#include <stdio.h>
  321. X#include "arc.h"
  322. X
  323. X#if    UNIX
  324. X#include <sys/types.h>
  325. X#include <sys/stat.h>
  326. X#endif
  327. X
  328. X#include <string.h>
  329. X#if BSD
  330. X#include <strings.h>
  331. X#endif
  332. X
  333. X#if    !__STDC__
  334. char        *calloc(), *malloc(), *realloc();
  335. X#endif
  336. X
  337. VOID        addarc(), delarc(), extarc(), lstarc(), tstarc(), cvtarc(), runarc();
  338. VOID        arcdie();
  339. static    VOID    expandlst();
  340. X#if    _MTS
  341. VOID        etoa();
  342. X#endif
  343. X#if    GEMDOS
  344. long        _stksize = 30720;
  345. X#endif
  346. char        *makefnam();    /* filename fixup routine */
  347. X
  348. static char   **lst;        /* files list */
  349. static int    lnum;        /* length of files list */
  350. X
  351. main(num, arg)            /* system entry point */
  352. X    int        num;    /* number of arguments */
  353. X    char           *arg[];    /* pointers to arguments */
  354. X{
  355. X    char        opt = 0;/* selected action */
  356. X    char           *a;    /* option pointer */
  357. X    VOID        upper();/* case conversion routine */
  358. X    char           *envfind();    /* environment searcher */
  359. X    int        n;    /* index */
  360. X    char           *arctemp2, *mktemp();
  361. X#if    GEMDOS
  362. X    VOID        exitpause();
  363. X    int        append;
  364. X#endif
  365. X#if    _MTS
  366. X    fortran VOID    guinfo();
  367. X    char        gotinf[4];
  368. X#endif
  369. X#if    UNIX
  370. X    struct    stat    sbuf;
  371. X#endif
  372. X
  373. X    if (num < 3) {
  374. X        printf("ARC - Archive utility, Version 5.21e, created on 10/30/91 at 14:30:21\n");
  375. X/*        printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  376. X        printf(" ALL RIGHTS RESERVED\n\n");
  377. X        printf("Please refer all inquiries to:\n\n");
  378. X        printf("       System Enhancement Associates\n");
  379. X        printf("       21 New Street, Wayne NJ 07470\n\n");
  380. X        printf("You may copy and distribute this program freely,");
  381. X        printf(" provided that:\n");
  382. X        printf("    1)     No fee is charged for such copying and");
  383. X        printf(" distribution, and\n");
  384. X        printf("    2)     It is distributed ONLY in its original,");
  385. X        printf(" unmodified state.\n\n");
  386. X        printf("If you like this program, and find it of use, then your");
  387. X        printf(" contribution will\n");
  388. X        printf("be appreciated.     You may not use this product in a");
  389. X        printf(" commercial environment\n");
  390. X        printf("or a governmental organization without paying a license");
  391. X        printf(" fee of $35.  Site\n");
  392. X        printf("licenses and commercial distribution licenses are");
  393. X        printf(" available.  A program\n");
  394. X        printf("disk and printed documentation are available for $50.\n");
  395. X        printf("\nIf you fail to abide by the terms of this license, ");
  396. X        printf(" then your conscience\n");
  397. X        printf("will haunt you for the rest of your life.\n\n"); */
  398. X#if    MSDOS
  399. X        printf("Usage: ARC {amufdxerplvtc}[bswnoq][g<password>]");
  400. X#endif
  401. X#if    GEMDOS
  402. X        printf("Usage: ARC {amufdxerplvtc}[bhswnoq][g<password>]");
  403. X#endif
  404. X#if    UNIX
  405. X        printf("Usage: arc {amufdxerplvtc}[biswnoq][g<password>]");
  406. X#endif
  407. X#if    _MTS
  408. X        printf("Parameters: {amufdxeplvtc}[biswnoq][g<password>]");
  409. X#endif
  410. X        printf(" <archive> [<filename> . . .]\n");
  411. X        printf("Where:     a   = add files to archive\n");
  412. X        printf("     m   = move files to archive\n");
  413. X        printf("     u   = update files in archive\n");
  414. X        printf("     f   = freshen files in archive\n");
  415. X        printf("     d   = delete files from archive\n");
  416. X        printf("     x,e = extract files from archive\n");
  417. X#if    !_MTS
  418. X        printf("     r   = run files from archive\n");
  419. X#endif
  420. X        printf("     p   = copy files from archive to");
  421. X        printf(" standard output\n");
  422. X        printf("     l   = list files in archive\n");
  423. X        printf("     v   = verbose listing of files in archive\n");
  424. X        printf("     t   = test archive integrity\n");
  425. X        printf("     c   = convert entry to new packing method\n");
  426. X        printf("     b   = retain backup copy of archive\n");
  427. X#if    GEMDOS
  428. X        printf("     h   = hold screen after finishing\n");
  429. X#endif
  430. X#if    _MTS
  431. X        printf("     i   = suppress ASCII/EBCDIC translation\n");
  432. X#endif
  433. X#if    UNIX
  434. X        printf("     i   = suppress image mode (translate EOL)\n");
  435. X#endif
  436. X        printf("     s   = suppress compression (store only)\n");
  437. X        printf("     w   = suppress warning messages\n");
  438. X        printf("     n   = suppress notes and comments\n");
  439. X        printf("     o   = overwrite existing files when");
  440. X        printf(" extracting\n");
  441. X        printf("     q   = squash instead of crunching\n");
  442. X        printf("     g   = Encrypt/decrypt archive entry\n");
  443. X        printf("\nAdapted from MSDOS by Howard Chu\n");
  444. X        /*
  445. X         * printf("\nPlease refer to the program documentation for");
  446. X         * printf(" complete instructions.\n"); 
  447. X         */
  448. X#if    GEMDOS
  449. X        exitpause();
  450. X#endif
  451. X        return 1;
  452. X    }
  453. X    /* see where temp files go */
  454. X#if    !_MTS
  455. X    arctemp = calloc(1, STRLEN);
  456. X    if (!(arctemp2 = envfind("ARCTEMP")))
  457. X        arctemp2 = envfind("TMPDIR");
  458. X    if (arctemp2) {
  459. X        strcpy(arctemp, arctemp2);
  460. X        n = strlen(arctemp);
  461. X        if (arctemp[n - 1] != CUTOFF)
  462. X            arctemp[n] = CUTOFF;
  463. X    }
  464. X#if    UNIX
  465. X    else    strcpy(arctemp, "/tmp/");
  466. X#endif
  467. X#if    !MSDOS
  468. X    {
  469. X        static char tempname[] = "AXXXXXX";
  470. X        strcat(arctemp, mktemp(tempname));
  471. X    }
  472. X#else
  473. X    strcat(arctemp, "$ARCTEMP");
  474. X#endif
  475. X#else
  476. X    guinfo("SHFSEP    ", gotinf);
  477. X    sepchr[0] = gotinf[0];
  478. X    guinfo("SCRFCHAR", gotinf);
  479. X    tmpchr[0] = gotinf[0];
  480. X    arctemp = "-$$$";
  481. X    arctemp[0] = tmpchr[0];
  482. X#endif
  483. X    arctemp2 = NULL;
  484. X
  485. X#if    !UNIX
  486. X    /* avoid any case problems with arguments */
  487. X
  488. X    for (n = 1; n < num; n++)    /* for each argument */
  489. X        upper(arg[n]);    /* convert it to uppercase */
  490. X#else
  491. X    /* avoid case problems with command options */
  492. X    upper(arg[1]);        /* convert to uppercase */
  493. X#endif
  494. X
  495. X    /* create archive names, supplying defaults */
  496. X#if    UNIX
  497. X    if (!stat(arg[2],&sbuf)) {
  498. X        if ((sbuf.st_mode & S_IFMT) == S_IFDIR)
  499. X            makefnam(arg[2],".arc",arcname);
  500. X        else
  501. X            strcpy(arcname,arg[2]);
  502. X    } else
  503. X        makefnam(arg[2],".arc",arcname);
  504. X#else
  505. X    makefnam(arg[2], ".ARC", arcname);
  506. X#endif
  507. X    /* makefnam(".$$$",arcname,newname); */
  508. X    sprintf(newname, "%s.arc", arctemp);
  509. X    makefnam(".BAK", arcname, bakname);
  510. X
  511. X    /* now scan the command and see what we are to do */
  512. X
  513. X    for (a = arg[1]; *a; a++) {    /* scan the option flags */
  514. X#if    !_MTS
  515. X        if (index("AMUFDXEPLVTCR", *a)) {    /* if a known command */
  516. X#else
  517. X        if (index("AMUFDXEPLVTC", *a)) {
  518. X#endif
  519. X            if (opt)/* do we have one yet? */
  520. X                arcdie("Cannot mix %c and %c", opt, *a);
  521. X            opt = *a;    /* else remember it */
  522. X        } else if (*a == 'B')    /* retain backup copy */
  523. X            keepbak = 1;
  524. X
  525. X        else if (*a == 'W')    /* suppress warnings */
  526. X            warn = 0;
  527. X#if    !DOS
  528. X        else if (*a == 'I')    /* image mode, no ASCII/EBCDIC x-late */
  529. X            image = !image;
  530. X#endif
  531. X#if    GEMDOS
  532. X        else if (*a == 'H')    /* pause before exit */
  533. X            hold = 1;
  534. X#endif
  535. X
  536. X        else if (*a == 'N')    /* suppress notes and comments */
  537. X            note = 0;
  538. X
  539. X        else if (*a == 'O')    /* overwrite file on extract */
  540. X            overlay = 1;
  541. X
  542. X        else if (*a == 'G') {    /* garble */
  543. X            password = a + 1;
  544. X            while (*a)
  545. X                a++;
  546. X            a--;
  547. X#if    _MTS
  548. X            etoa(password, strlen(password));
  549. X#endif
  550. X        } else if (*a == 'S')    /* storage kludge */
  551. X            nocomp = 1;
  552. X
  553. X        else if (*a == 'K')    /* special kludge */
  554. X            kludge = 1;
  555. X
  556. X        else if (*a == 'Q')    /* use squashing */
  557. X            dosquash = 1;
  558. X
  559. X        else if (*a == '-' || *a == '/')    /* UNIX and PC-DOS
  560. X                             * option markers */
  561. X            ;
  562. X
  563. X        else
  564. X            arcdie("%c is an unknown command", *a);
  565. X    }
  566. X
  567. X    if (!opt)
  568. X        arcdie("I have nothing to do!");
  569. X
  570. X    /* get the files list set up */
  571. X
  572. X    lnum = num - 3;        /* initial length of list */
  573. X    lst = (char **) calloc((lnum==0) ? 1:lnum,
  574. X                 sizeof(char *));    /* initial list */
  575. X    for (n = 3; n < num; n++)
  576. X        lst[n - 3] = arg[n];
  577. X
  578. X    for (n = 0; n < lnum;) {/* expand indirect references */
  579. X        if (*lst[n] == '@')
  580. X            expandlst(n);
  581. X#if    GEMDOS        /* redirect stdout from the desktop...*/
  582. X        else if (*lst[n] == '>') {
  583. X            arctemp2 = (++lst[n]);
  584. X            lst[n] = lst[--lnum];    /* delete this entry */
  585. X            if (arctemp2[0] == '>') {
  586. X                append = 1;
  587. X                arctemp2++;
  588. X            }
  589. X            else    append = 0;
  590. X        }
  591. X#endif
  592. X        else
  593. X            n++;
  594. X    }
  595. X#if    GEMDOS
  596. X    if (arctemp2)
  597. X        freopen(arctemp2,append ? "a" : "w",stdout);
  598. X#endif
  599. X
  600. X    /* act on whatever action command was given */
  601. X
  602. X    switch (opt) {        /* action depends on command */
  603. X    case 'A':        /* Add */
  604. X    case 'M':        /* Move */
  605. X    case 'U':        /* Update */
  606. X    case 'F':        /* Freshen */
  607. X        addarc(lnum, lst, (opt == 'M'), (opt == 'U'), (opt == 'F'));
  608. X        break;
  609. X
  610. X    case 'D':        /* Delete */
  611. X        delarc(lnum, lst);
  612. X        break;
  613. X
  614. X    case 'E':        /* Extract */
  615. X    case 'X':        /* eXtract */
  616. X    case 'P':        /* Print */
  617. X        extarc(lnum, lst, (opt == 'P'));
  618. X        break;
  619. X
  620. X    case 'V':        /* Verbose list */
  621. X        bose = 1;
  622. X    case 'L':        /* List */
  623. X        lstarc(lnum, lst);
  624. X        break;
  625. X
  626. X    case 'T':        /* Test */
  627. X        tstarc();
  628. X        break;
  629. X
  630. X    case 'C':        /* Convert */
  631. X        cvtarc(lnum, lst);
  632. X        break;
  633. X#if    !_MTS
  634. X    case 'R':        /* Run */
  635. X        runarc(lnum, lst);
  636. X        break;
  637. X#endif
  638. X    default:
  639. X        arcdie("I don't know how to do %c yet!", opt);
  640. X    }
  641. X#if    GEMDOS
  642. X    if (hold)
  643. X        exitpause();
  644. X#endif
  645. X    return nerrs;
  646. X}
  647. static    VOID
  648. expandlst(n)            /* expand an indirect reference */
  649. X    int        n;    /* number of entry to expand */
  650. X{
  651. X    FILE           *lf, *fopen();    /* list file, opener */
  652. X    char        buf[100];    /* input buffer */
  653. X    int        x;    /* index */
  654. X    char           *p = lst[n] + 1; /* filename pointer */
  655. X
  656. X    if (*p) {        /* use name if one was given */
  657. X        makefnam(p, ".CMD", buf);
  658. X        if (!(lf = fopen(buf, "r")))
  659. X            arcdie("Cannot read list of files in %s", buf);
  660. X    } else
  661. X        lf = stdin;    /* else use standard input */
  662. X
  663. X    for (x = n + 1; x < lnum; x++)    /* drop reference from the list */
  664. X        lst[x - 1] = lst[x];
  665. X    lnum--;
  666. X
  667. X    while (fscanf(lf, "%99s", buf) > 0) {    /* read in the list */
  668. X        if (!(lst =(char **)realloc(lst, (lnum + 1) * sizeof(char *))))
  669. X            arcdie("too many file references");
  670. X
  671. X        lst[lnum] = malloc(strlen(buf) + 1);
  672. X        strcpy(lst[lnum], buf); /* save the name */
  673. X        lnum++;
  674. X    }
  675. X
  676. X    if (lf != stdin)    /* avoid closing standard input */
  677. X        fclose(lf);
  678. X}
  679. END_OF_FILE
  680. if test 11929 -ne `wc -c <'arc.c'`; then
  681.     echo shar: \"'arc.c'\" unpacked with wrong size!
  682. fi
  683. # end of 'arc.c'
  684. fi
  685. if test -f 'arcadd.c' -a "${1}" != "-c" ; then 
  686.   echo shar: Will not clobber existing file \"'arcadd.c'\"
  687. else
  688. echo shar: Extracting \"'arcadd.c'\" \(10176 characters\)
  689. sed "s/^X//" >'arcadd.c' <<'END_OF_FILE'
  690. X/*
  691. X * $Header: /var/local/hyc/src/arc/RCS/arcadd.c,v 2.0 1991/11/12 02:31:25 hyc Exp $
  692. X */
  693. X
  694. X/*
  695. X * ARC - Archive utility - ARCADD
  696. X * 
  697. X * Version 3.40, created on 06/18/86 at 13:10:18
  698. X * 
  699. X * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  700. X * 
  701. X * By:  Thom Henderson
  702. X * 
  703. X * Description: This file contains the routines used to add files to an archive.
  704. X * 
  705. X * Language: Computer Innovations Optimizing C86
  706. X */
  707. X#include <stdio.h>
  708. X#include "arc.h"
  709. X#if    _MTS
  710. X#include <mts.h>
  711. X#include <ctype.h>
  712. X#endif
  713. X#include <string.h>
  714. X#if    BSD
  715. X#include <strings.h>
  716. X#endif
  717. X
  718. static    int    addfile();
  719. int    readhdr(), unlink();
  720. X#if    UNIX
  721. int    izadir();
  722. X#endif
  723. VOID    writehdr(), filecopy(), getstamp();
  724. VOID    pack(), closearc(), openarc(), arcdie();
  725. X
  726. X#ifndef    __STDC__
  727. char           *malloc(), *realloc();    /* memory allocators */
  728. X#ifndef _AIX
  729. VOID    free();
  730. X#endif
  731. X#endif
  732. X
  733. VOID
  734. addarc(num, arg, move, update, fresh)        /* add files to archive */
  735. X    int             num;    /* number of arguments */
  736. X    char           *arg[];    /* pointers to arguments */
  737. int             move;        /* true if moving file */
  738. int             update;        /* true if updating */
  739. int             fresh;        /* true if freshening */
  740. X{
  741. X    char           *d, *dir();    /* directory junk */
  742. X    char            buf[STRLEN];    /* pathname buffer */
  743. X    char          **path;    /* pointer to pointers to paths */
  744. X    char          **name;    /* pointer to pointers to names */
  745. X    int             nfiles = 0;    /* number of files in lists */
  746. X    int             notemp;    /* true until a template works */
  747. X    int             nowork = 1;    /* true until files are added */
  748. X    char           *i;    /* string indexing junk */
  749. X    int             n;    /* index */
  750. X#if    MSDOS
  751. X    unsigned int    coreleft();    /* remaining memory reporter */
  752. X#endif
  753. X    int        addbunch();
  754. X
  755. X    if (num < 1) {        /* if no files named */
  756. X        num = 1;    /* then fake one */
  757. X#if    DOS
  758. X        arg[0] = "*.*";    /* add everything */
  759. X#endif
  760. X#if    UNIX
  761. X        arg[0] = "*";
  762. X#endif
  763. X#if    _MTS
  764. X        arg[0] = "?";
  765. X#endif
  766. X    }
  767. X    path = (char **) malloc(sizeof(char **));
  768. X    name = (char **) malloc(sizeof(char **));
  769. X
  770. X
  771. X    for (n = 0; n < num; n++) {    /* for each template supplied */
  772. X        strcpy(buf, arg[n]);    /* get ready to fix path */
  773. X#if    !_MTS
  774. X        if (!(i = rindex(buf, '\\')))
  775. X            if (!(i = rindex(buf, '/')))
  776. X                if (!(i = rindex(buf, ':')))
  777. X                    i = buf - 1;
  778. X#else
  779. X        if (!(i = rindex(buf, sepchr[0])))
  780. X            if (buf[0] != tmpchr[0])
  781. X                i = buf - 1;
  782. X            else
  783. X                i = buf;
  784. X#endif
  785. X        i++;        /* pointer to where name goes */
  786. X
  787. X        notemp = 1;    /* reset files flag */
  788. X        for (d = dir(arg[n]); d; d = dir(NULL)) {
  789. X            notemp = 0;    /* template is giving results */
  790. X            nfiles++;    /* add each matching file */
  791. X            path = (char **) realloc(path, nfiles * sizeof(char **));
  792. X            name = (char **) realloc(name, nfiles * sizeof(char **));
  793. X            strcpy(i, d);    /* put name in path */
  794. X            path[nfiles - 1] = malloc(strlen(buf) + 1);
  795. X            strcpy(path[nfiles - 1], buf);
  796. X            name[nfiles - 1] = d;    /* save name */
  797. X#if    MSDOS
  798. X            if (coreleft() < 5120) {
  799. X                nfiles = addbunch(nfiles, path, name, move, update, fresh);
  800. X                nowork = nowork && !nfiles;
  801. X                while (nfiles) {
  802. X                    free(path[--nfiles]);
  803. X                    free(name[nfiles]);
  804. X                }
  805. X                free(path);
  806. X                free(name);
  807. X                path = name = NULL;
  808. X            }
  809. X#endif
  810. X        }
  811. X        if (notemp && warn)
  812. X            printf("No files match: %s\n", arg[n]);
  813. X    }
  814. X
  815. X    if (nfiles) {
  816. X        nfiles = addbunch(nfiles, path, name, move, update, fresh);
  817. X        nowork = nowork && !nfiles;
  818. X        while (nfiles) {
  819. X            free(path[--nfiles]);
  820. X            free(name[nfiles]);
  821. X        }
  822. X        free(path);
  823. X        free(name);
  824. X    }
  825. X    if (nowork && warn)
  826. X        printf("No files were added.\n");
  827. X}
  828. X
  829. int
  830. addbunch(nfiles, path, name, move, update, fresh)    /* add a bunch of files */
  831. X    int             nfiles;    /* number of files to add */
  832. X    char          **path;    /* pointers to pathnames */
  833. X    char          **name;    /* pointers to filenames */
  834. X    int             move;    /* true if moving file */
  835. X    int             update;    /* true if updating */
  836. X    int             fresh;    /* true if freshening */
  837. X{
  838. X    int             m, n;    /* indices */
  839. X    char           *d;    /* swap pointer */
  840. X    struct heads    hdr;    /* file header data storage */
  841. X
  842. X    for (n = 0; n < nfiles - 1; n++) {    /* sort the list of names */
  843. X        for (m = n + 1; m < nfiles; m++) {
  844. X            if (strcmp(name[n], name[m]) > 0) {
  845. X                d = path[n];
  846. X                path[n] = path[m];
  847. X                path[m] = d;
  848. X                d = name[n];
  849. X                name[n] = name[m];
  850. X                name[m] = d;
  851. X            }
  852. X        }
  853. X    }
  854. X
  855. X    for (n = 0; n < nfiles - 1;) {    /* consolidate the list of names */
  856. X        if (!strcmp(path[n], path[n + 1])    /* if duplicate names */
  857. X            ||!strcmp(path[n], arcname)    /* or this archive */
  858. X#if    UNIX
  859. X            ||izadir(path[n])    /* or a directory */
  860. X#endif
  861. X            ||!strcmp(path[n], newname)    /* or the new version */
  862. X            ||!strcmp(path[n], bakname)) {    /* or its backup */
  863. X            free(path[n]);    /* then forget the file */
  864. X            free(name[n]);
  865. X            for (m = n; m < nfiles - 1; m++) {
  866. X                path[m] = path[m + 1];
  867. X                name[m] = name[m + 1];
  868. X            }
  869. X            nfiles--;
  870. X        } else
  871. X            n++;    /* else test the next one */
  872. X    }
  873. X
  874. X    if (!strcmp(path[n], arcname)    /* special check for last file */
  875. X        ||!strcmp(path[n], newname)    /* courtesy of Rick Moore */
  876. X#if    UNIX
  877. X        ||izadir(path[n])
  878. X#endif
  879. X        || !strcmp(path[n], bakname)) {
  880. X        free(path[n]);
  881. X        free(name[n]);
  882. X        nfiles--;
  883. X    }
  884. X    if (!nfiles)        /* make sure we got some */
  885. X        return 0;
  886. X
  887. X    for (n = 0; n < nfiles - 1; n++) {    /* watch out for duplicate
  888. X                         * names */
  889. X        if (!strcmp(name[n], name[n + 1]))
  890. X            arcdie("Duplicate filenames:\n  %s\n  %s", path[n], path[n + 1]);
  891. X    }
  892. X    openarc(1);        /* open archive for changes */
  893. X
  894. X    for (n = 0; n < nfiles;) { /* add each file in the list */
  895. X        if (addfile(path[n], name[n], update, fresh) < 0) {
  896. X            free(path[n]);        /* remove this name if */
  897. X            free(name[n]);        /* it wasn't added */
  898. X            for (m = n; m < nfiles-1 ; m++) {
  899. X                path[m] = path[m+1];
  900. X                name[m] = name[m+1];
  901. X            }
  902. X            nfiles--;
  903. X        } else n++;
  904. X    }
  905. X
  906. X    /* now we must copy over all files that follow our additions */
  907. X
  908. X    while (readhdr(&hdr, arc)) {    /* while more entries to copy */
  909. X        writehdr(&hdr, new);
  910. X        filecopy(arc, new, hdr.size);
  911. X    }
  912. X    hdrver = 0;        /* archive EOF type */
  913. X    writehdr(&hdr, new);    /* write out our end marker */
  914. X    closearc(1);        /* close archive after changes */
  915. X
  916. X    if (move) {        /* if this was a move */
  917. X        for (n = 0; n < nfiles; n++) {    /* then delete each file
  918. X                         * added */
  919. X            if (unlink(path[n]) && warn) {
  920. X                printf("Cannot unsave %s\n", path[n]);
  921. X                nerrs++;
  922. X            }
  923. X        }
  924. X    }
  925. X    return nfiles;        /* say how many were added */
  926. X}
  927. X
  928. static          int
  929. addfile(path, name, update, fresh)    /* add named file to archive */
  930. X    char           *path;    /* path name of file to add */
  931. X    char           *name;    /* name of file to add */
  932. X    int             update;    /* true if updating */
  933. X    int             fresh;    /* true if freshening */
  934. X{
  935. X    struct heads    nhdr;    /* data regarding the new file */
  936. X    struct heads    ohdr;    /* data regarding an old file */
  937. X    FILE           *f, *fopen();    /* file to add, opener */
  938. X    long            starts, ftell();    /* file locations */
  939. X    int             upd = 0;/* true if replacing an entry */
  940. X
  941. X#if    !_MTS
  942. X    if (!(f = fopen(path, OPEN_R)))
  943. X#else
  944. X    if (image)
  945. X        f = fopen(path, OPEN_R);
  946. X    else
  947. X        f = fopen(path, "r");
  948. X    if (!f)
  949. X#endif
  950. X    {
  951. X        if (warn) {
  952. X            printf("Cannot read file: %s\n", path);
  953. X            nerrs++;
  954. X        }
  955. X        return(-1);
  956. X    }
  957. X#if    !DOS
  958. X    if (strlen(name) >= FNLEN) {
  959. X        if (warn) {
  960. X            char    buf[STRLEN];
  961. X            printf("WARNING: File %s name too long!\n", name);
  962. X            name[FNLEN-1]='\0';
  963. X            while(1) {
  964. X                printf("  Truncate to %s (y/n)? ", name);
  965. X                fflush(stdout);
  966. X                fgets(buf, STRLEN, stdin);
  967. X                *buf = toupper(*buf);
  968. X                if (*buf == 'Y' || *buf == 'N')
  969. X                    break;
  970. X            }
  971. X            if (*buf == 'N') {
  972. X                printf("Skipping...\n");
  973. X                fclose(f);
  974. X                return(-1);
  975. X            }
  976. X        }
  977. X        else {
  978. X            if (note)
  979. X                printf("Skipping file: %s - name too long.\n",
  980. X                    name);
  981. X            fclose(f);
  982. X            return(-1);
  983. X        }
  984. X    }
  985. X#endif
  986. X    strcpy(nhdr.name, name);/* save name */
  987. X    nhdr.size = 0;        /* clear out size storage */
  988. X    nhdr.crc = 0;        /* clear out CRC check storage */
  989. X#if    !_MTS
  990. X    getstamp(f, &nhdr.date, &nhdr.time);
  991. X#else
  992. X    {
  993. X    int    inlen;
  994. X    struct    GDDSECT    *region;
  995. X
  996. X    region=gdinfo(f->_fd._fdub);
  997. X    inlen=region->GDINLEN;
  998. X    buf=malloc(inlen);    /* maximum line length */
  999. X    setbuffer(f,buf,inlen);
  1000. X    f->_mods|=_NOIC;    /* Don't do "$continue with" */
  1001. X    f->_mods&=~_IC;        /* turn it off, if set... */
  1002. X    }
  1003. X    getstamp(path, &nhdr.date, &nhdr.time);
  1004. X#endif
  1005. X
  1006. X    /* position archive to spot for new file */
  1007. X
  1008. X    if (arc) {        /* if adding to existing archive */
  1009. X        starts = ftell(arc);    /* where are we? */
  1010. X        while (readhdr(&ohdr, arc)) {    /* while more files to check */
  1011. X            if (!strcmp(ohdr.name, nhdr.name)) {
  1012. X                upd = 1;    /* replace existing entry */
  1013. X                if (update || fresh) {    /* if updating or
  1014. X                             * freshening */
  1015. X                    if (nhdr.date < ohdr.date
  1016. X                        || (nhdr.date == ohdr.date && nhdr.time <= ohdr.time)) {
  1017. X                        fseek(arc, starts, 0);
  1018. X                        fclose(f);
  1019. X                        return(0);/* skip if !newer */
  1020. X                    }
  1021. X                }
  1022. X            }
  1023. X            if (strcmp(ohdr.name, nhdr.name) >= 0)
  1024. X                break;    /* found our spot */
  1025. X
  1026. X            writehdr(&ohdr, new);    /* entry preceeds update;
  1027. X                         * keep it */
  1028. X            filecopy(arc, new, ohdr.size);
  1029. X            starts = ftell(arc);    /* now where are we? */
  1030. X        }
  1031. X
  1032. X        if (upd) {    /* if an update */
  1033. X            if (note) {
  1034. X                printf("Updating file: %-12s  ", name);
  1035. X                fflush(stdout);
  1036. X            }
  1037. X            fseek(arc, ohdr.size, 1);
  1038. X        } else if (fresh) {    /* else if freshening */
  1039. X            fseek(arc, starts, 0);    /* then do not add files */
  1040. X            fclose(f);
  1041. X            return(0);
  1042. X        } else {    /* else adding a new file */
  1043. X            if (note) {
  1044. X                printf("Adding file:   %-12s  ", name);
  1045. X                fflush(stdout);
  1046. X            }
  1047. X            fseek(arc, starts, 0);    /* reset for next time */
  1048. X        }
  1049. X    } else {        /* no existing archive */
  1050. X        if (fresh) {    /* cannot freshen nothing */
  1051. X            fclose(f);
  1052. X            return(0);
  1053. X        } else if (note) {    /* else adding a file */
  1054. X            printf("Adding file:   %-12s  ", name);
  1055. X            fflush(stdout);
  1056. X        }
  1057. X    }
  1058. X
  1059. X    starts = ftell(new);    /* note where header goes */
  1060. X    hdrver = ARCVER;        /* anything but end marker */
  1061. X    writehdr(&nhdr, new);    /* write out header skeleton */
  1062. X#if    _MTS
  1063. X    atoe(nhdr.name, FNLEN); /* writehdr translated this... */
  1064. X#endif
  1065. X    pack(f, new, &nhdr);    /* pack file into archive */
  1066. X    fseek(new, starts, 0);    /* move back to header skeleton */
  1067. X    writehdr(&nhdr, new);    /* write out real header */
  1068. X    fseek(new, nhdr.size, 1);    /* skip over data to next header */
  1069. X    fclose(f);        /* all done with the file */
  1070. X    return(0);
  1071. X}
  1072. END_OF_FILE
  1073. if test 10176 -ne `wc -c <'arcadd.c'`; then
  1074.     echo shar: \"'arcadd.c'\" unpacked with wrong size!
  1075. fi
  1076. # end of 'arcadd.c'
  1077. fi
  1078. if test -f 'arclzw.c' -a "${1}" != "-c" ; then 
  1079.   echo shar: Will not clobber existing file \"'arclzw.c'\"
  1080. else
  1081. echo shar: Extracting \"'arclzw.c'\" \(22078 characters\)
  1082. sed "s/^X//" >'arclzw.c' <<'END_OF_FILE'
  1083. X/*
  1084. X * $Header: /var/local/hyc/src/arc/RCS/arclzw.c,v 2.0 1991/11/12 00:30:01 hyc Exp $
  1085. X */
  1086. X
  1087. X/*
  1088. X * ARC - Archive utility - ARCLZW
  1089. X *
  1090. X * Version 2.03, created on 10/24/86 at 11:46:22
  1091. X *
  1092. X * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  1093. X *
  1094. X * By:  Thom Henderson
  1095. X *
  1096. X * Description: This file contains the routines used to implement Lempel-Zev
  1097. X * data compression, which calls for building a coding table on the fly.
  1098. X * This form of compression is especially good for encoding files which
  1099. X * contain repeated strings, and can often give dramatic improvements over
  1100. X * traditional Huffman SQueezing.
  1101. X *
  1102. X * Language: Computer Innovations Optimizing C86
  1103. X *
  1104. X * Programming notes: In this section I am drawing heavily on the COMPRESS
  1105. X * program from UNIX.  The basic method is taken from "A Technique for High
  1106. X * Performance Data Compression", Terry A. Welch, IEEE Computer Vol 17, No 6
  1107. X * (June 1984), pp 8-19.  Also see "Knuth's Fundamental Algorithms", Donald
  1108. X * Knuth, Vol 3, Section 6.4.
  1109. X *
  1110. X * As best as I can tell, this method works by tracing down a hash table of code
  1111. X * strings where each entry has the property:
  1112. X *
  1113. X * if <string> <char> is in the table then <string> is in the table.
  1114. X */
  1115. X#include <stdio.h>
  1116. X#include "arc.h"
  1117. X
  1118. VOID            arcdie();
  1119. X#if    MSDOS
  1120. char           *setmem();
  1121. X#else
  1122. X#if    NEEDMEMSET
  1123. char           *memset();
  1124. X#else
  1125. X#include <memory.h>
  1126. X#endif
  1127. X#endif
  1128. X
  1129. X#include "proto.h"
  1130. static VOID     putcode();
  1131. X/* definitions for older style crunching */
  1132. X
  1133. X#define FALSE    0
  1134. X#define TRUE     !FALSE
  1135. X#define TABSIZE  4096
  1136. X#define NO_PRED  0xFFFF
  1137. X#define EMPTY    0xFFFF
  1138. X#define NOT_FND  0xFFFF
  1139. X
  1140. extern u_char    pinbuf[MYBUF];
  1141. u_char        *inbeg, *inend;
  1142. u_char           outbuf[MYBUF];
  1143. u_char          *outbeg, *outend = &outbuf[MYBUF - 1];
  1144. X
  1145. static int      sp;        /* current stack pointer */
  1146. static int    inflag;
  1147. X
  1148. struct entry {            /* string table entry format */
  1149. X    char            used;    /* true when this entry is in use */
  1150. X    u_char           follower;    /* char following string */
  1151. X    u_short          next;    /* ptr to next in collision list */
  1152. X    u_short          predecessor;    /* code for preceeding string */
  1153. X};                /* string_tab[TABSIZE];       the code string
  1154. X                 * table */
  1155. X
  1156. X
  1157. X/* definitions for the new dynamic Lempel-Zev crunching */
  1158. X
  1159. X#define CRBITS    12        /* maximum bits per code */
  1160. X#define CRHSIZE    5003        /* 80% occupancy */
  1161. X#define    CRGAP    2048        /* ratio check interval */
  1162. X#define    SQBITS    13        /* Squash values of above */
  1163. X#define    SQHSIZE    10007
  1164. X#define    SQGAP    10000
  1165. X#define INIT_BITS 9        /* initial number of bits/code */
  1166. X
  1167. static int      Bits;
  1168. static int      Hsize;
  1169. static int      Check_Gap;
  1170. X
  1171. static int      n_bits;        /* number of bits/code */
  1172. static int      maxcode;    /* maximum code, given n_bits */
  1173. X#define MAXCODE(n)      ((1<<(n)) - 1)    /* maximum code calculation */
  1174. static int      max_maxcode;    /* 1 << BITS; largest possible code (+1) */
  1175. X
  1176. static char     buf[SQBITS];    /* input/output buffer */
  1177. X
  1178. static u_char    lmask[9] =    /* left side masks */
  1179. X{
  1180. X    0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00
  1181. X};
  1182. static u_char    rmask[9] =    /* right side masks */
  1183. X{
  1184. X    0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
  1185. X};
  1186. X
  1187. static int      offset;        /* byte offset for code output */
  1188. static long     in_count;    /* length of input */
  1189. static int      in_off;        /* where to start reading input */
  1190. static long     bytes_out;    /* length of compressed output */
  1191. static long     bytes_last;    /* previous output size */
  1192. static u_short   ent;
  1193. static long     fcode;
  1194. static int      hshift;
  1195. X
  1196. X/*
  1197. X * To save much memory (which we badly need at this point), we overlay the
  1198. X * table used by the previous version of Lempel-Zev with those used by the
  1199. X * new version.  Since no two of these routines will be used together, we can
  1200. X * safely do this.
  1201. X */
  1202. X
  1203. long            htab[SQHSIZE];    /* hash code table   (crunch) */
  1204. u_short          codetab[SQHSIZE];    /* string code table (crunch) */
  1205. static struct entry *string_tab = (struct entry *) htab;    /* old crunch string
  1206. X                                 * table */
  1207. X
  1208. static u_short  *prefix = codetab;    /* prefix code table (uncrunch) */
  1209. static u_char   *suffix = (u_char *) htab;    /* suffix table (uncrunch) */
  1210. X
  1211. static int      free_ent;    /* first unused entry */
  1212. u_char          *stack = (u_char *) & htab[1 << SQBITS];
  1213. X /* local push/pop stack */
  1214. X
  1215. X/*
  1216. X * block compression parameters -- after all codes are used up, and
  1217. X * compression rate changes, start over.
  1218. X */
  1219. X
  1220. static int      clear_flg;
  1221. static long     ratio;
  1222. static long     checkpoint;
  1223. VOID            upd_tab();
  1224. X
  1225. X/*
  1226. X * the next two codes should not be changed lightly, as they must not lie
  1227. X * within the contiguous general code space.
  1228. X */
  1229. X#define FIRST   257        /* first free entry */
  1230. X#define CLEAR   256        /* table clear output code */
  1231. X
  1232. X/*
  1233. X * The cl_block() routine is called at each checkpoint to determine if
  1234. X * compression would likely improve by resetting the code table.
  1235. X */
  1236. X
  1237. static VOID
  1238. cl_block(t)            /* table clear for block compress */
  1239. X    FILE           *t;    /* our output file */
  1240. X{
  1241. X    long            rat;
  1242. X
  1243. X    checkpoint = in_count + Check_Gap;
  1244. X
  1245. X    if (in_count > 0x007fffffL) {    /* shift will overflow */
  1246. X        rat = bytes_out >> 8;
  1247. X        if (rat == 0)    /* Don't divide by zero */
  1248. X            rat = 0x7fffffffL;
  1249. X        else
  1250. X            rat = in_count / rat;
  1251. X    } else
  1252. X        rat = (in_count << 8) / bytes_out;    /* 8 fractional bits */
  1253. X
  1254. X    if (rat > ratio)
  1255. X        ratio = rat;
  1256. X    else {
  1257. X        ratio = 0;
  1258. X        setmem(htab, Hsize * sizeof(long), 0xff);
  1259. X        free_ent = FIRST;
  1260. X        clear_flg = 1;
  1261. X        putcode(CLEAR, t);
  1262. X    }
  1263. X}
  1264. X
  1265. X#define FLUSH_BUF(bytes)    \
  1266. do {    bytes_out += bytes;\
  1267. X    outbeg += bytes;\
  1268. X    if (outend - outbeg < Bits) {\
  1269. X        putb_pak(outbuf, (u_int) (bytes_out - bytes_last), t);\
  1270. X        bytes_last = bytes_out;\
  1271. X        outbeg = outbuf;\
  1272. X    }\
  1273. X    offset = 0;\
  1274. X} while (0)
  1275. X
  1276. X/*****************************************************************
  1277. X *
  1278. X * Output a given code.
  1279. X * Inputs:
  1280. X *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
  1281. X *              that n_bits =< (LONG)wordsize - 1.
  1282. X * Outputs:
  1283. X *      Outputs code to the file.
  1284. X * Assumptions:
  1285. X *      Chars are 8 bits long.
  1286. X * Algorithm:
  1287. X *      Maintain a BITS character long buffer (so that 8 codes will
  1288. X * fit in it exactly).  When the buffer fills up empty it and start over.
  1289. X */
  1290. X
  1291. static VOID
  1292. putcode(code, t)        /* output a code */
  1293. X    int             code;    /* code to output */
  1294. X    FILE           *t;    /* where to put it */
  1295. X{
  1296. X    int             r_off = offset;    /* right offset */
  1297. X    int             bits = n_bits;    /* bits to go */
  1298. X    u_char          *bp = outbeg;    /* buffer pointer */
  1299. X
  1300. X    register int    ztmp;
  1301. X
  1302. X    bp += (r_off >> 3);    /* Get to the first byte. */
  1303. X    r_off &= 7;
  1304. X
  1305. X    /*
  1306. X     * Since code is always >= 8 bits, only need to mask the
  1307. X     * first hunk on the left.
  1308. X     */
  1309. X    ztmp = (code << r_off) & lmask[r_off];
  1310. X    *bp = (*bp & rmask[r_off]) | ztmp;
  1311. X    bp++;
  1312. X    bits -= (8 - r_off);
  1313. X    code >>= (8 - r_off);
  1314. X
  1315. X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1316. X    if (bits >= 8) {
  1317. X        *bp++ = code;
  1318. X        code >>= 8;
  1319. X        bits -= 8;
  1320. X    }
  1321. X    /* Last bits. */
  1322. X    if (bits)
  1323. X        *bp = code;
  1324. X    offset += n_bits;
  1325. X
  1326. X    if (offset == (n_bits << 3))
  1327. X        FLUSH_BUF(n_bits);
  1328. X    /*
  1329. X     * If the next entry is going to be too big for the code
  1330. X     * size, then increase it, if possible.
  1331. X     */
  1332. X    if (free_ent > maxcode || clear_flg > 0) {
  1333. X        /*
  1334. X         * Write the whole buffer, because the input side
  1335. X         * won't discover the size increase until after it
  1336. X         * has read it.
  1337. X         */
  1338. X        if (offset > 0)
  1339. X            FLUSH_BUF(n_bits);
  1340. X
  1341. X        if (clear_flg) {    /* reset if clearing */
  1342. X            maxcode = MAXCODE(n_bits = INIT_BITS);
  1343. X            clear_flg = 0;
  1344. X        } else {/* else use more bits */
  1345. X            n_bits++;
  1346. X            if (n_bits == Bits)
  1347. X                maxcode = max_maxcode;
  1348. X            else
  1349. X                maxcode = MAXCODE(n_bits);
  1350. X        }
  1351. X    }
  1352. X}
  1353. X
  1354. X/*****************************************************************
  1355. X *
  1356. X * Read codes from the input file.  If EOF, return -1.
  1357. X * Inputs:
  1358. X *      cmpin
  1359. X * Outputs:
  1360. X *      code or -1 is returned.
  1361. X */
  1362. X
  1363. static int
  1364. getcode(f)            /* get a code */
  1365. X    FILE           *f;    /* file to get from */
  1366. X{
  1367. X    int             code;
  1368. X    static int      size = 0;
  1369. X    int             r_off, bits;
  1370. X    u_char          *bp = (u_char *) buf;
  1371. X
  1372. X    if (clear_flg > 0 || offset >= size || free_ent > maxcode) {
  1373. X        /*
  1374. X         * If the next entry will be too big for the current code
  1375. X         * size, then we must increase the size. This implies reading
  1376. X         * a new buffer full, too.
  1377. X         */
  1378. X        if (free_ent > maxcode) {
  1379. X            n_bits++;
  1380. X            if (n_bits == Bits)
  1381. X                maxcode = max_maxcode;    /* won't get any bigger
  1382. X                             * now */
  1383. X            else
  1384. X                maxcode = MAXCODE(n_bits);
  1385. X        }
  1386. X        if (clear_flg > 0) {
  1387. X            maxcode = MAXCODE(n_bits = INIT_BITS);
  1388. X            clear_flg = 0;
  1389. X        }
  1390. X        for (size = 0; size < n_bits; size++) {
  1391. X            if (inbeg >= inend) {
  1392. X                u_int inlen = getb_unp(f);
  1393. X                if (inlen == 0) {
  1394. X                    code = EOF;
  1395. X                    break;
  1396. X                } else {
  1397. X                    inbeg = pinbuf;
  1398. X                    inend = &inbeg[inlen];
  1399. X                }
  1400. X            }
  1401. X            code = *inbeg++;
  1402. X            buf[size] = (char) code;
  1403. X        }
  1404. X        if (size <= 0)
  1405. X            return -1;    /* end of file */
  1406. X
  1407. X        offset = 0;
  1408. X        /* Round size down to integral number of codes */
  1409. X        size = (size << 3) - (n_bits - 1);
  1410. X    }
  1411. X    r_off = offset;
  1412. X    bits = n_bits;
  1413. X
  1414. X    /*
  1415. X     * Get to the first byte.
  1416. X     */
  1417. X    bp += (r_off >> 3);
  1418. X    r_off &= 7;
  1419. X
  1420. X    /* Get first part (low order bits) */
  1421. X    code = (*bp++ >> r_off);
  1422. X    bits -= 8 - r_off;
  1423. X    r_off = 8 - r_off;    /* now, offset into code word */
  1424. X
  1425. X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1426. X    if (bits >= 8) {
  1427. X        code |= *bp++ << r_off;
  1428. X        r_off += 8;
  1429. X        bits -= 8;
  1430. X    }
  1431. X    /* high order bits. */
  1432. X    code |= (*bp & rmask[bits]) << r_off;
  1433. X    offset += n_bits;
  1434. X
  1435. X    return code & MAXCODE(Bits);
  1436. X}
  1437. X
  1438. X/*
  1439. X * compress a file
  1440. X *
  1441. X * Algorithm:  use open addressing double hashing (no chaining) on the prefix
  1442. X * code / next character combination.  We do a variant of Knuth's algorithm D
  1443. X * (vol. 3, sec. 6.4) along with G. Knott's relatively-prime secondary probe.
  1444. X * Here, the modular division first probe is gives way to a faster
  1445. X * exclusive-or manipulation.  Also do block compression with an adaptive
  1446. X * reset, where the code table is cleared when the compression ratio
  1447. X * decreases, but after the table fills.  The variable-length output codes
  1448. X * are re-sized at this point, and a special CLEAR code is generated for the
  1449. X * decompressor.
  1450. X */
  1451. X
  1452. VOID
  1453. init_cm(buf)            /* initialize for compression */
  1454. X    u_char          *buf;    /* input buffer */
  1455. X{
  1456. X    offset = 0;
  1457. X    clear_flg = 0;
  1458. X    ratio = 0;
  1459. X    in_count = 1;
  1460. X    in_off = 1;
  1461. X    maxcode = MAXCODE(n_bits = INIT_BITS);
  1462. X    free_ent = FIRST;
  1463. X    n_bits = INIT_BITS;    /* set starting code size */
  1464. X    outbeg = outbuf;
  1465. X    bytes_last = 0;
  1466. X
  1467. X    if (dosquash) {
  1468. X        Bits = SQBITS;
  1469. X        Hsize = SQHSIZE;
  1470. X        Check_Gap = SQGAP;
  1471. X        bytes_out = 0;
  1472. X    } else {
  1473. X        Bits = CRBITS;
  1474. X        Hsize = CRHSIZE;
  1475. X        Check_Gap = CRGAP;
  1476. X        bytes_out = 1;
  1477. X        *outbeg++ = Bits;    /* note our max code length */
  1478. X    }
  1479. X    checkpoint = Check_Gap;
  1480. X    max_maxcode = 1 << Bits;
  1481. X    setmem(htab, Hsize * sizeof(long), 0xff);
  1482. X
  1483. X    ent = *buf;
  1484. X    hshift = 0;
  1485. X    for (fcode = (long) Hsize; fcode < 65536L; fcode *= 2L)
  1486. X        hshift++;
  1487. X    hshift = 8 - hshift;
  1488. X}
  1489. X
  1490. VOID
  1491. lzw_buf(buf, len, t)        /* compress a character */
  1492. X    u_char          *buf;    /* buffer to compress */
  1493. X    u_int            len;    /* length of buffer */
  1494. X    FILE           *t;    /* where to put it */
  1495. X{
  1496. X    int             i, j;
  1497. X    int             disp;
  1498. X
  1499. X    j = in_off;
  1500. X    buf += in_off;
  1501. X    in_off = 0;
  1502. X    for (; j < len; j++, buf++) {
  1503. X        in_count++;
  1504. X
  1505. X        fcode = (long) (((long) *buf << Bits) + ent);
  1506. X        i = (*buf << hshift) ^ ent;    /* xor hashing */
  1507. X
  1508. X        if (htab[i] == fcode) {
  1509. X            ent = codetab[i];
  1510. X            continue;
  1511. X        } else if (htab[i] < 0)    /* empty slot */
  1512. X            goto nomatch;
  1513. X        disp = Hsize - i;    /* secondary hash (after G.Knott) */
  1514. X        if (i == 0)
  1515. X            disp = 1;
  1516. X
  1517. probe:
  1518. X        if ((i -= disp) < 0)
  1519. X            i += Hsize;
  1520. X
  1521. X        if (htab[i] == fcode) {
  1522. X            ent = codetab[i];
  1523. X            continue;
  1524. X        }
  1525. X        if (htab[i] > 0)
  1526. X            goto probe;
  1527. X
  1528. nomatch:
  1529. X        putcode(ent, t);
  1530. X        ent = *buf;
  1531. X        if (free_ent < max_maxcode) {
  1532. X            codetab[i] = free_ent++;    /* code -> hashtable */
  1533. X            htab[i] = fcode;
  1534. X        } else if (in_count >= checkpoint)
  1535. X            cl_block(t);    /* check for adaptive reset */
  1536. X    }
  1537. X}
  1538. X
  1539. long
  1540. pred_cm(t)            /* report compressed size */
  1541. X    FILE           *t;    /* where to put it */
  1542. X{
  1543. X    putcode(ent, t);    /* put out the final code */
  1544. X
  1545. X    offset = (offset + 7) / 8;
  1546. X    bytes_out += offset;
  1547. X
  1548. X    return bytes_out;    /* say how big it got */
  1549. X}
  1550. X
  1551. VOID
  1552. flsh_cm(t)            /* flush compressed file */
  1553. X    FILE          *t;
  1554. X{
  1555. X    putb_pak(outbuf, (u_int) (bytes_out - bytes_last), t);
  1556. X}
  1557. X
  1558. X/*
  1559. X * Decompress a file.  This routine adapts to the codes in the file building
  1560. X * the string table on-the-fly; requiring no table to be stored in the
  1561. X * compressed file.  The tables used herein are shared with those of the
  1562. X * compress() routine.  See the definitions above.
  1563. X */
  1564. X
  1565. VOID
  1566. decomp(squash, f, t)        /* decompress a file */
  1567. X    int        squash;    /* squashed or crunched? */
  1568. X    FILE           *f;    /* file to read codes from */
  1569. X    FILE           *t;    /* file to write text to */
  1570. X{
  1571. X    u_char          *stackp;
  1572. X    int             finchar;
  1573. X    int             code, oldcode, incode;
  1574. X    VOID            (*output) PROTO((u_char *buf, u_int len, FILE *f));
  1575. X    u_int        inlen;
  1576. X
  1577. X    inlen = getb_unp(f);
  1578. X    inbeg = pinbuf;
  1579. X    inend = &inbeg[inlen];
  1580. X    outbeg = outbuf;
  1581. X
  1582. X    if (squash) {
  1583. X        Bits = SQBITS;
  1584. X        output = putb_unp;
  1585. X    } else {
  1586. X        Bits = CRBITS;
  1587. X        output = putb_ncr;
  1588. X        if ((code = *inbeg++) != CRBITS)
  1589. X            arcdie("File packed with %d bits, I can only handle %d",
  1590. X                  code, CRBITS);
  1591. X    }
  1592. X
  1593. X    if (inlen<=0)
  1594. X        return;
  1595. X
  1596. X    max_maxcode = 1 << Bits;
  1597. X    clear_flg = 0;
  1598. X
  1599. X    n_bits = INIT_BITS;    /* set starting code size */
  1600. X
  1601. X    /*
  1602. X     * As above, initialize the first 256 entries in the table.
  1603. X     */
  1604. X    maxcode = MAXCODE(n_bits);
  1605. X    setmem(prefix, 256 * sizeof(short), 0);    /* reset decode string table */
  1606. X    for (code = 255; code >= 0; code--)
  1607. X        suffix[code] = (u_char) code;
  1608. X
  1609. X    free_ent = FIRST;
  1610. X
  1611. X    finchar = oldcode = getcode(f);
  1612. X    if (oldcode == -1)    /* EOF already? */
  1613. X        return;        /* Get out of here */
  1614. X    *outbeg++ = finchar;    /* first code must be 8 bits=char */
  1615. X    stackp = stack;
  1616. X
  1617. X    while ((code = getcode(f)) > -1) {
  1618. X        if (code == CLEAR) {    /* reset string table */
  1619. X            setmem(prefix, 256 * sizeof(short), 0);
  1620. X            clear_flg = 1;
  1621. X            free_ent = FIRST - 1;
  1622. X            if ((code = getcode(f)) == -1)    /* O, untimely death! */
  1623. X                break;
  1624. X        }
  1625. X        incode = code;
  1626. X        /*
  1627. X         * Special case for KwKwK string.
  1628. X         */
  1629. X        if (code >= free_ent) {
  1630. X            if (code > free_ent) {
  1631. X                if (warn) {
  1632. X                    printf("Corrupted compressed file.\n");
  1633. X                    printf("Invalid code %d when max is %d.\n",
  1634. X                           code, free_ent);
  1635. X                }
  1636. X                nerrs++;
  1637. X                break;
  1638. X            }
  1639. X            *stackp++ = finchar;
  1640. X            code = oldcode;
  1641. X        }
  1642. X        /*
  1643. X         * Generate output characters in reverse order
  1644. X         */
  1645. X        while (code >= 256) {
  1646. X            *stackp++ = suffix[code];
  1647. X            code = prefix[code];
  1648. X        }
  1649. X        *stackp++ = finchar = suffix[code];
  1650. X
  1651. X        /*
  1652. X         * And put them out in forward order
  1653. X         */
  1654. X        do {
  1655. X            *outbeg++ = *--stackp;
  1656. X            if (outbeg >= outend) {
  1657. X                (*output) (outbuf, outbeg-outbuf, t);
  1658. X                outbeg = outbuf;
  1659. X            }
  1660. X        } while (stackp > stack);
  1661. X
  1662. X        /*
  1663. X         * Generate the new entry.
  1664. X         */
  1665. X        if ((code = free_ent) < max_maxcode) {
  1666. X            prefix[code] = (u_short) oldcode;
  1667. X            suffix[code] = finchar;
  1668. X            free_ent = code + 1;
  1669. X        }
  1670. X        /*
  1671. X         * Remember previous code.
  1672. X         */
  1673. X        oldcode = incode;
  1674. X    }
  1675. X
  1676. X    if (outbeg > outbuf)
  1677. X        (*output) (outbuf, outbeg-outbuf, t);
  1678. X}
  1679. X
  1680. X
  1681. X/*************************************************************************
  1682. X * Please note how much trouble it can be to maintain upwards            *
  1683. X * compatibility.  All that follows is for the sole purpose of unpacking *
  1684. X * files which were packed using an older method.                        *
  1685. X *************************************************************************/
  1686. X
  1687. X
  1688. X/*
  1689. X * The h() pointer points to the routine to use for calculating a hash value.
  1690. X * It is set in the init routines to point to either of oldh() or newh().
  1691. X *
  1692. X * oldh() calculates a hash value by taking the middle twelve bits of the square
  1693. X * of the key.
  1694. X *
  1695. X * newh() works somewhat differently, and was tried because it makes ARC about
  1696. X * 23% faster.  This approach was abandoned because dynamic Lempel-Zev
  1697. X * (above) works as well, and packs smaller also.  However, inadvertent
  1698. X * release of a developmental copy forces us to leave this in.
  1699. X */
  1700. X
  1701. static          u_short(*h) ();    /* pointer to hash function */
  1702. X
  1703. static          u_short
  1704. oldh(pred, foll)        /* old hash function */
  1705. X    u_short          pred;    /* code for preceeding string */
  1706. X    u_char           foll;    /* value of following char */
  1707. X{
  1708. X    long            local;    /* local hash value */
  1709. X
  1710. X    local = ((pred + foll) | 0x0800) & 0xFFFF;    /* create the hash key */
  1711. X    local *= local;        /* square it */
  1712. X    return (local >> 6) & 0x0FFF;    /* return the middle 12 bits */
  1713. X}
  1714. X
  1715. static          u_short
  1716. newh(pred, foll)        /* new hash function */
  1717. X    u_short          pred;    /* code for preceeding string */
  1718. X    u_char           foll;    /* value of following char */
  1719. X{
  1720. X    return (((pred + foll) & 0xFFFF) * 15073) & 0xFFF;    /* faster hash */
  1721. X}
  1722. X
  1723. X/*
  1724. X * The eolist() function is used to trace down a list of entries with
  1725. X * duplicate keys until the last duplicate is found.
  1726. X */
  1727. X
  1728. static          u_short
  1729. eolist(index)            /* find last duplicate */
  1730. X    u_short          index;
  1731. X{
  1732. X    int             temp;
  1733. X
  1734. X    while (temp = string_tab[index].next)    /* while more duplicates */
  1735. X        index = temp;
  1736. X
  1737. X    return index;
  1738. X}
  1739. X
  1740. X/*
  1741. X * The hash() routine is used to find a spot in the hash table for a new
  1742. X * entry.  It performs a "hash and linear probe" lookup, using h() to
  1743. X * calculate the starting hash value and eolist() to perform the linear
  1744. X * probe.  This routine DOES NOT detect a table full condition.  That MUST be
  1745. X * checked for elsewhere.
  1746. X */
  1747. X
  1748. static          u_short
  1749. hash(pred, foll)        /* find spot in the string table */
  1750. X    u_short          pred;    /* code for preceeding string */
  1751. X    u_char           foll;    /* char following string */
  1752. X{
  1753. X    u_short          local, tempnext;    /* scratch storage */
  1754. X    struct entry   *ep;    /* allows faster table handling */
  1755. X
  1756. X    local = (*h) (pred, foll);    /* get initial hash value */
  1757. X
  1758. X    if (!string_tab[local].used)    /* if that spot is free */
  1759. X        return local;    /* then that's all we need */
  1760. X
  1761. X    else {            /* else a collision has occured */
  1762. X        local = eolist(local);    /* move to last duplicate */
  1763. X
  1764. X        /*
  1765. X         * We must find an empty spot. We start looking 101 places
  1766. X         * down the table from the last duplicate.
  1767. X         */
  1768. X
  1769. X        tempnext = (local + 101) & 0x0FFF;
  1770. X        ep = &string_tab[tempnext];    /* initialize pointer */
  1771. X
  1772. X        while (ep->used) {    /* while empty spot not found */
  1773. X            if (++tempnext == TABSIZE) {    /* if we are at the end */
  1774. X                tempnext = 0;    /* wrap to beginning of table */
  1775. X                ep = string_tab;
  1776. X            } else
  1777. X                ++ep;    /* point to next element in table */
  1778. X        }
  1779. X
  1780. X        /*
  1781. X         * local still has the pointer to the last duplicate, while
  1782. X         * tempnext has the pointer to the spot we found.  We use
  1783. X         * this to maintain the chain of pointers to duplicates.
  1784. X         */
  1785. X
  1786. X        string_tab[local].next = tempnext;
  1787. X
  1788. X        return tempnext;
  1789. X    }
  1790. X}
  1791. X
  1792. X/*
  1793. X * The init_tab() routine is used to initialize our hash table. You realize,
  1794. X * of course, that "initialize" is a complete misnomer.
  1795. X */
  1796. X
  1797. static VOID
  1798. init_tab()
  1799. X{                /* set ground state in hash table */
  1800. X    unsigned int    i;    /* table index */
  1801. X
  1802. X    setmem((char *) string_tab, TABSIZE * sizeof(struct entry), 0);
  1803. X
  1804. X    for (i = 0; i < 256; i++)    /* list all single byte strings */
  1805. X        upd_tab(NO_PRED, i);
  1806. X}
  1807. X
  1808. X/*
  1809. X * The upd_tab routine is used to add a new entry to the string table. As
  1810. X * previously stated, no checks are made to ensure that the table has any
  1811. X * room.  This must be done elsewhere.
  1812. X */
  1813. X
  1814. VOID
  1815. upd_tab(pred, foll)        /* add an entry to the table */
  1816. X    u_short          pred;    /* code for preceeding string */
  1817. X    u_short          foll;    /* character which follows string */
  1818. X{
  1819. X    struct entry   *ep;    /* pointer to current entry */
  1820. X
  1821. X    /* calculate offset just once */
  1822. X
  1823. X    ep = &string_tab[hash(pred, foll)];
  1824. X
  1825. X    ep->used = TRUE;    /* this spot is now in use */
  1826. X    ep->next = 0;        /* no duplicates after this yet */
  1827. X    ep->predecessor = pred;    /* note code of preceeding string */
  1828. X    ep->follower = foll;    /* note char after string */
  1829. X}
  1830. X
  1831. X/*
  1832. X * This algorithm encoded a file into twelve bit strings (three nybbles). The
  1833. X * gocode() routine is used to read these strings a byte (or two) at a time.
  1834. X */
  1835. X
  1836. X#define    GOCODE(x)\
  1837. if ((inflag^=1)) { x = (*inbeg++ << 4); x |= (*inbeg >> 4); } \
  1838. else {x = (*inbeg++ & 0x0f) << 8; x |= (*inbeg++); }
  1839. X
  1840. X/* push char onto stack */
  1841. X#define    PUSH(c)    \
  1842. do {    stack[sp] = ((char) (c)); \
  1843. X    if (++sp >= TABSIZE) \
  1844. X        arcdie("Stack overflow\n"); \
  1845. X} while (0)
  1846. X
  1847. X/* pop character from stack */
  1848. X#define POP()    ((sp > 0) ? (int) stack[--sp] : EMPTY)
  1849. X
  1850. X/***** LEMPEL-ZEV DECOMPRESSION *****/
  1851. X
  1852. static int      code_count;    /* needed to detect table full */
  1853. static int      oldcode, finchar;
  1854. X
  1855. VOID
  1856. init_ucr(new, f)        /* get set for uncrunching */
  1857. X    int             new;    /* true to use new hash function */
  1858. X    FILE           *f;    /* input file */
  1859. X{
  1860. X    if (new)        /* set proper hash function */
  1861. X        h = newh;
  1862. X    else
  1863. X        h = oldh;
  1864. X
  1865. X    sp = 0;            /* clear out the stack */
  1866. X    init_tab();        /* set up atomic code definitions */
  1867. X    code_count = TABSIZE - 256;    /* note space left in table */
  1868. X    inbeg = pinbuf;
  1869. X    inend = &inbeg[getb_unp(f)];
  1870. X    inflag = 0;
  1871. X    GOCODE(oldcode);
  1872. X    finchar = string_tab[oldcode].follower;
  1873. X    outbeg = outbuf;
  1874. X    *outbeg++ = finchar;
  1875. X}
  1876. X
  1877. u_int
  1878. getb_ucr(f)            /* get next uncrunched byte */
  1879. X    FILE           *f;    /* file containing crunched data */
  1880. X{
  1881. X    int             code, newcode;
  1882. X    struct entry   *ep;    /* allows faster table handling */
  1883. X    u_int        len;
  1884. X
  1885. X    do {
  1886. X    if (!sp) {        /* if stack is empty */
  1887. X        if (inbeg >= inend-1) {
  1888. X            inbeg = pinbuf;
  1889. X            inend = &inbeg[getb_unp(f)];
  1890. X            if (inbeg == inend) {
  1891. X                break;
  1892. X            }
  1893. X        }
  1894. X        GOCODE(newcode);
  1895. X        code = newcode;
  1896. X
  1897. X        ep = &string_tab[code];    /* initialize pointer */
  1898. X
  1899. X        if (!ep->used) {/* if code isn't known */
  1900. X            code = oldcode;
  1901. X            ep = &string_tab[code];    /* re-initialize pointer */
  1902. X            PUSH(finchar);
  1903. X        }
  1904. X        while (ep->predecessor != NO_PRED) {
  1905. X            PUSH(ep->follower);    /* decode string backwards */
  1906. X            code = ep->predecessor;
  1907. X            ep = &string_tab[code];
  1908. X        }
  1909. X
  1910. X        PUSH(finchar = ep->follower);    /* save first character also */
  1911. X
  1912. X        /*
  1913. X         * The above loop will terminate, one way or another, with
  1914. X         * string_tab[code].follower equal to the first character in
  1915. X         * the string.
  1916. X         */
  1917. X
  1918. X        if (code_count) {    /* if room left in string table */
  1919. X            upd_tab(oldcode, finchar);
  1920. X            --code_count;
  1921. X        }
  1922. X        oldcode = newcode;
  1923. X    }
  1924. X    *outbeg++ = POP();
  1925. X    } while (outbeg <= outend);
  1926. X    len = outbeg - outbuf;
  1927. X    outbeg = outbuf;
  1928. X    return (len);
  1929. X}
  1930. END_OF_FILE
  1931. if test 22078 -ne `wc -c <'arclzw.c'`; then
  1932.     echo shar: \"'arclzw.c'\" unpacked with wrong size!
  1933. fi
  1934. # end of 'arclzw.c'
  1935. fi
  1936. if test -f 'arcmisc.c' -a "${1}" != "-c" ; then 
  1937.   echo shar: Will not clobber existing file \"'arcmisc.c'\"
  1938. else
  1939. echo shar: Extracting \"'arcmisc.c'\" \(8784 characters\)
  1940. sed "s/^X//" >'arcmisc.c' <<'END_OF_FILE'
  1941. X/*
  1942. X * Miscellaneous routines to get ARC running on non-MSDOS systems...
  1943. X * $Header: /var/local/hyc/src/arc/RCS/arcmisc.c,v 2.0 1991/11/12 00:34:06 hyc Exp $ 
  1944. X */
  1945. X
  1946. X#include <stdio.h>
  1947. X#include <ctype.h>
  1948. X#include "arc.h"
  1949. X
  1950. X#include <string.h>
  1951. X#if    BSD
  1952. X#include <strings.h>
  1953. X#endif
  1954. X
  1955. X#if    MSDOS
  1956. X#include <dir.h>
  1957. X#include <stat.h>
  1958. X#endif
  1959. X
  1960. X#if    GEMDOS
  1961. X#include <types.h>
  1962. X#include <osbind.h>
  1963. X#include <stat.h>
  1964. X
  1965. VOID 
  1966. exitpause()
  1967. X{
  1968. X    while (Cconis())
  1969. X        Cnecin();
  1970. X    fprintf(stderr, "Press any key to continue: ");
  1971. X    fflush(stderr);
  1972. X    Cnecin();
  1973. X    fprintf(stderr, "\n");
  1974. X}
  1975. X
  1976. int
  1977. chdir(dirname)
  1978. X    char           *dirname;
  1979. X{
  1980. X    char           *i;
  1981. X    int             drv;
  1982. X
  1983. X    i = dirname;
  1984. X    if ((i = index(dirname, ':')) != NULL) {
  1985. X        drv = i[-1];
  1986. X        i++;        /* Move past device spec */
  1987. X        if (drv > '\'')
  1988. X            drv -= 'a';
  1989. X        else
  1990. X            drv -= 'A';
  1991. X        if (drv >= 0 && drv < 16)
  1992. X            Dsetdrv(drv);
  1993. X    }
  1994. X    if (*i != '\0')
  1995. X        return (Dsetpath(i));
  1996. X}
  1997. X#endif
  1998. X
  1999. X#if    UNIX
  2000. X#include <sys/types.h>
  2001. X#if    SYSV
  2002. X#include <dirent.h>
  2003. X#define DIRECT dirent
  2004. X#else
  2005. X#include <sys/dir.h>
  2006. X#define DIRECT direct
  2007. X#endif
  2008. X#include <sys/stat.h>
  2009. X    int    rename(), unlink();
  2010. X#endif
  2011. X
  2012. X#if    NEEDMEMSET
  2013. char    *
  2014. memset(s, c, n)        /* This came from SVR2? */
  2015. X    char    *s;
  2016. X    int    c, n;
  2017. X{
  2018. X    register int i;
  2019. X    for(i=0;i<n;i++)
  2020. X        s[i]=c;
  2021. X    return(s);
  2022. X}
  2023. X#else
  2024. X#include <memory.h>
  2025. X#endif
  2026. X
  2027. X#ifndef    __STDC__
  2028. char        *malloc();
  2029. X#ifndef _AIX
  2030. int        free();
  2031. X#endif
  2032. X#endif
  2033. int             match();
  2034. X
  2035. int
  2036. move(oldnam, newnam)
  2037. X    char           *oldnam, *newnam;
  2038. X{
  2039. X    FILE           *fopen(), *old, *new;
  2040. X#if    !_MTS
  2041. X    struct stat     oldstat;
  2042. X#endif
  2043. X    VOID        filecopy();
  2044. X#if    GEMDOS
  2045. X    if (Frename(0, oldnam, newnam))
  2046. X#else
  2047. X    if (rename(oldnam, newnam))
  2048. X#endif
  2049. X#if    !_MTS
  2050. X    {
  2051. X        if (stat(oldnam, &oldstat))    /* different partition? */
  2052. X            return (-1);
  2053. X        old = fopen(oldnam, OPEN_R);
  2054. X        if (old == NULL)
  2055. X            return (-1);
  2056. X        new = fopen(newnam, OPEN_W);
  2057. X        if (new == NULL)
  2058. X            return (-1);
  2059. X        filecopy(old, new, oldstat.st_size);
  2060. X        return(unlink(oldnam));
  2061. X    }
  2062. X    return 0;
  2063. X#else
  2064. X    return(-1);
  2065. X#endif
  2066. X}
  2067. X
  2068. static VOID
  2069. X_makefn(source, dest)
  2070. X    char           *source;
  2071. X    char           *dest;
  2072. X{
  2073. X    int             j;
  2074. X#if    MSDOS
  2075. X    char           *setmem();
  2076. X#endif
  2077. X
  2078. X    setmem(dest, 17, 0);    /* clear result field */
  2079. X    for (j = 0; *source && *source != '.'; ++source)
  2080. X        if (j < 8)
  2081. X            dest[j++] = *source;
  2082. X    for (j = 9; *source; ++source)
  2083. X        if (j < 13)
  2084. X            dest[j++] = *source;
  2085. X}
  2086. X/*
  2087. X * make a file name using a template 
  2088. X */
  2089. X
  2090. char           *
  2091. makefnam(rawfn, template, result)
  2092. X    char           *rawfn;    /* the original file name */
  2093. X    char           *template;    /* the template data */
  2094. X    char           *result;    /* where to place the result */
  2095. X{
  2096. X    char            et[17], er[17], rawbuf[STRLEN], *i;
  2097. X
  2098. X    *rawbuf = 0;
  2099. X    strcpy(rawbuf, rawfn);
  2100. X#if    _MTS
  2101. X    i = rawbuf;
  2102. X    if (rawbuf[0] == tmpchr[0]) {
  2103. X        i++;
  2104. X        strcpy(rawfn, i);
  2105. X    } else
  2106. X#endif
  2107. X    if ((i = rindex(rawbuf, CUTOFF))) {
  2108. X        i++;
  2109. X        strcpy(rawfn, i);
  2110. X    }
  2111. X#if    DOS
  2112. X    else if ((i = rindex(rawbuf, ':'))) {
  2113. X        i++;
  2114. X        strcpy(rawfn, i);
  2115. X    }
  2116. X#endif
  2117. X    if (i)
  2118. X        *i = 0;
  2119. X    else
  2120. X        *rawbuf = 0;
  2121. X
  2122. X    _makefn(template, et);
  2123. X    _makefn(rawfn, er);
  2124. X    *result = 0;        /* assure no data */
  2125. X    strcat(result, rawbuf);
  2126. X    strcat(result, er[0] ? er : et);
  2127. X    strcat(result, er[9] ? er + 9 : et + 9);
  2128. X    return ((char *) &result[0]);
  2129. X}
  2130. X
  2131. X#if    MSDOS || SYSV
  2132. X
  2133. int
  2134. alphasort(dirptr1, dirptr2)
  2135. X    struct DIRECT **dirptr1, **dirptr2;
  2136. X{
  2137. X    return (strcmp((*dirptr1)->d_name, (*dirptr2)->d_name));
  2138. X}
  2139. X
  2140. X#endif
  2141. X
  2142. VOID
  2143. upper(string)
  2144. X    char           *string;
  2145. X{
  2146. X    char           *p;
  2147. X
  2148. X    for (p = string; *p; p++)
  2149. X        if (islower(*p))
  2150. X            *p = toupper(*p);
  2151. X}
  2152. X/* VARARGS1 */
  2153. VOID
  2154. arcdie(s, arg1, arg2, arg3)
  2155. X    char           *s;
  2156. X{
  2157. X    fprintf(stderr, "ARC: ");
  2158. X    fprintf(stderr, s, arg1, arg2, arg3);
  2159. X    fprintf(stderr, "\n");
  2160. X#if    UNIX
  2161. X    perror("UNIX");
  2162. X#endif
  2163. X#if    GEMDOS
  2164. X    exitpause();
  2165. X#endif
  2166. X    exit(1);
  2167. X}
  2168. X
  2169. X#if    !_MTS
  2170. X
  2171. char           *
  2172. gcdir(dirname)
  2173. X    char           *dirname;
  2174. X
  2175. X{
  2176. X    char           *getwd();
  2177. X#if    GEMDOS
  2178. X    int             drv;
  2179. X    char           *buf;
  2180. X#endif
  2181. X    if (dirname == NULL || strlen(dirname) == 0)
  2182. X        dirname = (char *) malloc(1024);
  2183. X
  2184. X#if    !GEMDOS
  2185. X    getwd(dirname);
  2186. X#else
  2187. X    buf = dirname;
  2188. X    *buf++ = (drv = Dgetdrv()) + 'A';
  2189. X    *buf++ = ':';
  2190. X    Dgetpath(buf, 0);
  2191. X#endif
  2192. X    return (dirname);
  2193. X}
  2194. X
  2195. X#if    UNIX
  2196. char           *pattern;    /* global so that fmatch can use it */
  2197. X#endif
  2198. X
  2199. char           *
  2200. dir(filename)        /* get files, one by one */
  2201. X    char           *filename;    /* template, or NULL */
  2202. X{
  2203. X#if    GEMDOS
  2204. X    static int      Nnum = 0;
  2205. X#if    __GNUC__
  2206. X#define    d_fname    dta_name    /* Wish these libraries would agree on names */
  2207. X#define DMABUFFER    _DTA
  2208. X#endif
  2209. X    static DMABUFFER dbuf, *saved;
  2210. X    char           *name;
  2211. X    if (Nnum == 0) {    /* first call */
  2212. X        saved = (DMABUFFER *) Fgetdta();
  2213. X        Fsetdta(&dbuf);
  2214. X        if (Fsfirst(filename, 0) == 0) {
  2215. X            name = malloc(FNLEN);
  2216. X            strcpy(name, dbuf.d_fname);
  2217. X            Nnum++;
  2218. X            return (name);
  2219. X        } else {
  2220. X            Fsetdta(saved);
  2221. X            return (NULL);
  2222. X        }
  2223. X    } else {
  2224. X        if (Fsnext() == 0) {
  2225. X            name = malloc(FNLEN);
  2226. X            strcpy(name, dbuf.d_fname);
  2227. X            return (name);
  2228. X        } else {
  2229. X            Nnum = 0;
  2230. X            Fsetdta(saved);
  2231. X            return (NULL);
  2232. X        }
  2233. X    }
  2234. X}
  2235. X#else
  2236. X    static struct DIRECT **namelist;
  2237. X    static char   **NameList;
  2238. X    static char    namecopy[STRLEN], *dirname;
  2239. X#if    UNIX
  2240. X    int             alphasort();
  2241. X    int             scandir();
  2242. X#endif                /* UNIX */
  2243. X    int             fmatch();
  2244. X    static int      Nnum = 0, ii;
  2245. X
  2246. X
  2247. X    if (Nnum == 0) {    /* first call */
  2248. X        strcpy(namecopy,filename);
  2249. X        if(pattern=rindex(namecopy,CUTOFF)) {
  2250. X            *pattern = 0;
  2251. X            pattern++;
  2252. X            dirname = namecopy;
  2253. X        } else {
  2254. X            pattern = filename;
  2255. X            dirname = ".";
  2256. X        }
  2257. X        Nnum = scandir(dirname, &namelist, fmatch, alphasort);
  2258. X        NameList = (char **) malloc(Nnum * sizeof(char *));
  2259. X        for (ii = 0; ii < Nnum; ii++) {
  2260. X            (NameList)[ii] = malloc(strlen(namelist[ii]->d_name) + 1);
  2261. X            strcpy((NameList)[ii], namelist[ii]->d_name);
  2262. X        }
  2263. X        ii = 0;
  2264. X    }
  2265. X    if (ii >= Nnum) {    /* all out of files */
  2266. X        if (Nnum) {    /* there were some files found */
  2267. X            for (ii = 0; ii < Nnum; ii++)
  2268. X                free(namelist[ii]);
  2269. X            free(namelist);
  2270. X        }
  2271. X        Nnum = 0;
  2272. X        return (NULL);
  2273. X    } else {
  2274. X        return ((NameList)[ii++]);
  2275. X    }
  2276. X}
  2277. X
  2278. X/*
  2279. X * Filename match - here, * matches everything 
  2280. X */
  2281. X
  2282. int
  2283. fmatch(direntry)
  2284. X    struct DIRECT  *direntry;
  2285. X{
  2286. X    char           *string;
  2287. X
  2288. X    string = direntry->d_name;
  2289. X
  2290. X    if (!strcmp(pattern, "") || !strcmp(pattern, "*.*") || !strcmp(pattern, "*"))
  2291. X        return (1);
  2292. X    return (match(string, pattern));
  2293. X}
  2294. X#endif                /* GEMDOS */
  2295. X#else
  2296. X/* dir code for MTS under Bell Labs C... */
  2297. X
  2298. char           *
  2299. dir(filepattern)
  2300. X    char           *filepattern;    /* template or NULL */
  2301. X{
  2302. X#if    USECATSCAN
  2303. X    fortran VOID    catscan(), fileinfo();
  2304. X
  2305. X    struct catname {
  2306. X        short           len;
  2307. X        char            name[257];
  2308. X    }               pattern;
  2309. X
  2310. X    struct catval {
  2311. X        int             maxlen;
  2312. X        int             actlen;
  2313. X        char            name[257];
  2314. X    }               catreturn;
  2315. X
  2316. X    char           *i;
  2317. X    int             j, RETCODE;
  2318. X
  2319. X    static int      catptr = 0;
  2320. X    static int      catflag = 0x200;
  2321. X    static int      cattype = 1;
  2322. X    static int      patflag = 0;
  2323. X
  2324. X    catreturn.maxlen = 256;
  2325. X
  2326. X    if (patflag) {
  2327. X        patflag = 0;
  2328. X        catptr = 0;
  2329. X        return (NULL);
  2330. X    }
  2331. X    if (filepattern) {
  2332. X        strcpy(pattern.name, filepattern);
  2333. X        pattern.len = strlen(filepattern);
  2334. X        if (!index(filepattern, '?'))
  2335. X            patflag = 1;
  2336. X    }
  2337. X    if (patflag) {
  2338. X        fileinfo(&pattern, &cattype, "CINAME  ", &catreturn, _retcode RETCODE);
  2339. X        catptr = RETCODE ? 0 : 1;
  2340. X    } else
  2341. X        catscan(&pattern, &catflag, &cattype, &catreturn, &catptr);
  2342. X
  2343. X    if (!catptr)
  2344. X        return (NULL);
  2345. X    else {
  2346. X        char           *k;
  2347. X
  2348. X/*        k = index(catreturn.name, ' ');
  2349. X        if (k)
  2350. X            *k = 0;
  2351. X        else {        This seems unnecessary now    */
  2352. X            j = catreturn.actlen;
  2353. X            catreturn.name[j] = 0;
  2354. X/*        }        */
  2355. X        k = catreturn.name;
  2356. X        if (*k == tmpchr[0])
  2357. X            k++;
  2358. X        else if ((k = index(catreturn.name, sepchr[0])))
  2359. X            k++;
  2360. X        else
  2361. X            k = catreturn.name;
  2362. X        j = strlen(k);
  2363. X        i = malloc(++j);
  2364. X        strcpy(i, k);
  2365. X        return (i);
  2366. X    }
  2367. X#else
  2368. X    fortran VOID    gfinfo();
  2369. X    static char     gfname[24];
  2370. X    static char     pattern[20];
  2371. X    static int      gfdummy[2] = {
  2372. X                      0, 0
  2373. X    },              gfflags;
  2374. X    int             i, RETCODE;
  2375. X    char           *j, *k;
  2376. X
  2377. X    if (filepattern) {
  2378. X        strcpy(pattern, filepattern);
  2379. X        strcat(pattern, " ");
  2380. X        for (i = 20; i < 24; i++)
  2381. X            gfname[i] = '\0';
  2382. X        if (index(pattern, '?'))
  2383. X            gfflags = 0x0C;
  2384. X        else
  2385. X            gfflags = 0x09;
  2386. X    } else if (gfflags == 0x09)
  2387. X        return (NULL);
  2388. X
  2389. X    gfinfo(pattern, gfname, &gfflags, gfdummy, gfdummy, gfdummy, _retcode RETCODE);
  2390. X    if (RETCODE)
  2391. X        return (NULL);
  2392. X    else {
  2393. X        k = index(gfname, ' ');
  2394. X        *k = '\0';
  2395. X        k = gfname;
  2396. X        if (gfname[0] == tmpchr[0])
  2397. X            k++;
  2398. X        else if ((k = index(gfname, sepchr[0])))
  2399. X            k++;
  2400. X        else
  2401. X            k = gfname;
  2402. X        i = strlen(k);
  2403. X        j = malloc(++i);
  2404. X        strcpy(j, k);
  2405. X        return (j);
  2406. X    }
  2407. X#endif
  2408. X}
  2409. X
  2410. int
  2411. unlink(path)
  2412. X    char           *path;    /* name of file to delete */
  2413. X{
  2414. X    fortran VOID    destroy();
  2415. X    int             RETCODE;
  2416. X
  2417. X    char            name[258];
  2418. X
  2419. X    strcpy(name, path);
  2420. X    strcat(name, " ");
  2421. X    destroy(name, _retcode RETCODE);
  2422. X    if (RETCODE)
  2423. X        return (-1);
  2424. X    else
  2425. X        return (0);
  2426. X}
  2427. X#endif
  2428. END_OF_FILE
  2429. if test 8784 -ne `wc -c <'arcmisc.c'`; then
  2430.     echo shar: \"'arcmisc.c'\" unpacked with wrong size!
  2431. fi
  2432. # end of 'arcmisc.c'
  2433. fi
  2434. if test -f 'arcsq.c' -a "${1}" != "-c" ; then 
  2435.   echo shar: Will not clobber existing file \"'arcsq.c'\"
  2436. else
  2437. echo shar: Extracting \"'arcsq.c'\" \(13957 characters\)
  2438. sed "s/^X//" >'arcsq.c' <<'END_OF_FILE'
  2439. X/*
  2440. X * $Header: /var/local/hyc/src/arc/RCS/arcsq.c,v 2.0 1991/11/12 00:30:01 hyc Exp $
  2441. X */
  2442. X
  2443. X/*
  2444. X * ARC - Archive utility - ARCSQ
  2445. X *
  2446. X * Version 3.10, created on 01/30/86 at 20:10:46
  2447. X *
  2448. X * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  2449. X *
  2450. X * By:    Thom Henderson
  2451. X *
  2452. X * Description: This file contains the routines used to squeeze a file when
  2453. X * placing it in an archive.
  2454. X *
  2455. X * Language: Computer Innovations Optimizing C86
  2456. X *
  2457. X * Programming notes: Most of the routines used for the Huffman squeezing
  2458. X * algorithm were lifted from the SQ program by Dick Greenlaw, as adapted to
  2459. X * CI-C86 by Robert J. Beilstein.
  2460. X */
  2461. X#include <stdio.h>
  2462. X#include "arc.h"
  2463. X
  2464. X#include "proto.h"
  2465. X
  2466. X/* stuff for Huffman squeezing */
  2467. X
  2468. X#define TRUE 1
  2469. X#define FALSE 0
  2470. X#define ERROR (-1)
  2471. X#define SPEOF 256        /* special endfile token */
  2472. X#define NOCHILD (-1)        /* marks end of path through tree */
  2473. X#define NUMVALS 257        /* 256 data values plus SPEOF */
  2474. X#define NUMNODES (NUMVALS+NUMVALS-1)    /* number of nodes */
  2475. X#define MAXCOUNT (u_short) 65535/* biggest unsigned integer */
  2476. X
  2477. X/*
  2478. X * The following array of structures are the nodes of the binary trees. The
  2479. X * first NUMVALS nodes become the leaves of the final tree and represent the
  2480. X * values of the data bytes being encoded and the special endfile, SPEOF. The
  2481. X * remaining nodes become the internal nodes of the final tree.
  2482. X */
  2483. X
  2484. struct nd {            /* shared by unsqueezer */
  2485. X    u_short        weight; /* number of appearances */
  2486. X    short        tdepth; /* length on longest path in tree */
  2487. X    short        lchild, rchild; /* indices to next level */
  2488. X}        node[NUMNODES]; /* use large buffer */
  2489. X
  2490. static int    dctreehd;    /* index to head of final tree */
  2491. X
  2492. X/*
  2493. X * This is the encoding table: The bit strings have first bit in low bit.
  2494. X * Note that counts were scaled so code fits unsigned integer.
  2495. X */
  2496. X
  2497. static int    codelen[NUMVALS];    /* number of bits in code */
  2498. static u_short    code[NUMVALS];    /* code itself, right adjusted */
  2499. static u_short    tcode;        /* temporary code value */
  2500. static long    valcount[NUMVALS];    /* actual count of times seen */
  2501. X
  2502. X/* Variables used by encoding process */
  2503. X
  2504. int    curin;        /* value currently being encoded */
  2505. static int    cbitsrem;    /* # of code string bits left */
  2506. static u_short    ccode;        /* current code right justified */
  2507. X
  2508. static VOID    scale(), heap(), adjust(), bld_tree(), init_enc();
  2509. static int    cmptrees(), buildenc();
  2510. X
  2511. extern u_char    outbuf[], *outbeg, *outend;
  2512. X
  2513. VOID
  2514. init_sq()
  2515. X{                /* prepare for scanning pass */
  2516. X    int        i;    /* node index */
  2517. X
  2518. X    /*
  2519. X     * Initialize all nodes to single element binary trees with zero
  2520. X     * weight and depth.
  2521. X     */
  2522. X
  2523. X    for (i = 0; i < NUMNODES; ++i) {
  2524. X        node[i].weight = 0;
  2525. X        node[i].tdepth = 0;
  2526. X        node[i].lchild = NOCHILD;
  2527. X        node[i].rchild = NOCHILD;
  2528. X    }
  2529. X
  2530. X    for (i = 0; i < NUMVALS; i++)
  2531. X        valcount[i] = 0;
  2532. X}
  2533. X
  2534. VOID
  2535. hufb_tab(buf, len)        /* add a byte to the tables */
  2536. X    u_char           *buf;
  2537. X    u_int        len;
  2538. X{
  2539. X    for (; len > 0; len--) {
  2540. X
  2541. X        /* Build frequency info in tree */
  2542. X
  2543. X        if (node[*buf].weight != MAXCOUNT)
  2544. X            node[*buf].weight++;    /* bump weight counter */
  2545. X
  2546. X        valcount[*buf++]++;    /* bump byte counter */
  2547. X    }
  2548. X}
  2549. X
  2550. long
  2551. pred_sq()
  2552. X{                /* predict size of squeezed file */
  2553. X    int        i;
  2554. X    int        btlist[NUMVALS];    /* list of intermediate
  2555. X                         * b-trees */
  2556. X    int        listlen;/* length of btlist */
  2557. X    u_short        ceiling;/* limit for scaling */
  2558. X    long        size = 0;    /* predicted size */
  2559. X    int        numnodes;    /* # of nodes in simplified tree */
  2560. X
  2561. X    node[SPEOF].weight = 1; /* signal end of input */
  2562. X    valcount[SPEOF] = 1;
  2563. X
  2564. X    ceiling = MAXCOUNT;
  2565. X
  2566. X    /* Keep trying to scale and encode */
  2567. X
  2568. X    do {
  2569. X        scale(ceiling);
  2570. X        ceiling /= 2;    /* in case we rescale */
  2571. X
  2572. X        /*
  2573. X         * Build list of single node binary trees having leaves for
  2574. X         * the input values with non-zero counts
  2575. X         */
  2576. X
  2577. X        for (i = listlen = 0; i < NUMVALS; ++i) {
  2578. X            if (node[i].weight != 0) {
  2579. X                node[i].tdepth = 0;
  2580. X                btlist[listlen++] = i;
  2581. X            }
  2582. X        }
  2583. X
  2584. X        /*
  2585. X         * Arrange list of trees into a heap with the entry indexing
  2586. X         * the node with the least weight at the top.
  2587. X         */
  2588. X
  2589. X        heap(btlist, listlen);
  2590. X
  2591. X        /* Convert the list of trees to a single decoding tree */
  2592. X
  2593. X        bld_tree(btlist, listlen);
  2594. X
  2595. X        /* Initialize the encoding table */
  2596. X
  2597. X        init_enc();
  2598. X
  2599. X        /*
  2600. X         * Try to build encoding table. Fail if any code is > 16 bits
  2601. X         * long.
  2602. X         */
  2603. X    } while (buildenc(0, dctreehd) == ERROR);
  2604. X
  2605. X    /* Initialize encoding variables */
  2606. X
  2607. X    cbitsrem = 0;        /* force initial read */
  2608. X    curin = 0;        /* anything but endfile */
  2609. X
  2610. X    for (i = 0; i < NUMVALS; i++)    /* add bits for each code */
  2611. X        size += valcount[i] * codelen[i];
  2612. X
  2613. X    size = (size + 7) / 8;    /* reduce to number of bytes */
  2614. X
  2615. X    numnodes = dctreehd < NUMVALS ? 0 : dctreehd - (NUMVALS - 1);
  2616. X
  2617. X    size += sizeof(short) + 2 * numnodes * sizeof(short);
  2618. X
  2619. X    return size;
  2620. X}
  2621. X
  2622. X/*
  2623. X * The count of number of occurrances of each input value have already been
  2624. X * prevented from exceeding MAXCOUNT. Now we must scale them so that their
  2625. X * sum doesn't exceed ceiling and yet no non-zero count can become zero. This
  2626. X * scaling prevents errors in the weights of the interior nodes of the
  2627. X * Huffman tree and also ensures that the codes will fit in an unsigned
  2628. X * integer. Rescaling is used if necessary to limit the code length.
  2629. X */
  2630. X
  2631. static VOID
  2632. scale(ceil)
  2633. X    u_short        ceil;    /* upper limit on total weight */
  2634. X{
  2635. X    register int    i;
  2636. X    int        ovflw, divisor;
  2637. X    u_short        w, sum;
  2638. X    unsigned char    increased;    /* flag */
  2639. X
  2640. X    do {
  2641. X        for (i = sum = ovflw = 0; i < NUMVALS; ++i) {
  2642. X            if (node[i].weight > (ceil - sum))
  2643. X                ++ovflw;
  2644. X            sum += node[i].weight;
  2645. X        }
  2646. X
  2647. X        divisor = ovflw + 1;
  2648. X
  2649. X        /* Ensure no non-zero values are lost */
  2650. X
  2651. X        increased = FALSE;
  2652. X        for (i = 0; i < NUMVALS; ++i) {
  2653. X            w = node[i].weight;
  2654. X            if (w < divisor && w != 0) {    /* Don't fail to provide
  2655. X                             * a code if it's used
  2656. X                             * at all */
  2657. X
  2658. X                node[i].weight = divisor;
  2659. X                increased = TRUE;
  2660. X            }
  2661. X        }
  2662. X    } while (increased);
  2663. X
  2664. X    /* Scaling factor chosen, now scale */
  2665. X
  2666. X    if (divisor > 1)
  2667. X        for (i = 0; i < NUMVALS; ++i)
  2668. X            node[i].weight /= divisor;
  2669. X}
  2670. X
  2671. X/*
  2672. X * heap() and adjust() maintain a list of binary trees as a heap with the top
  2673. X * indexing the binary tree on the list which has the least weight or, in
  2674. X * case of equal weights, least depth in its longest path. The depth part is
  2675. X * not strictly necessary, but tends to avoid long codes which might provoke
  2676. X * rescaling.
  2677. X */
  2678. X
  2679. static VOID
  2680. heap(list, length)
  2681. X    int        list[], length;
  2682. X{
  2683. X    register int    i;
  2684. X
  2685. X    for (i = (length - 2) / 2; i >= 0; --i)
  2686. X        adjust(list, i, length - 1);
  2687. X}
  2688. X
  2689. X/* Make a heap from a heap with a new top */
  2690. X
  2691. static VOID
  2692. adjust(list, top, bottom)
  2693. X    int        list[], top, bottom;
  2694. X{
  2695. X    register int    k, temp;
  2696. X
  2697. X    k = 2 * top + 1;    /* left child of top */
  2698. X    temp = list[top];    /* remember root node of top tree */
  2699. X
  2700. X    if (k <= bottom) {
  2701. X        if (k < bottom && cmptrees(list[k], list[k + 1]))
  2702. X            ++k;
  2703. X
  2704. X        /* k indexes "smaller" child (in heap of trees) of top */
  2705. X        /* now make top index "smaller" of old top and smallest child */
  2706. X
  2707. X        if (cmptrees(temp, list[k])) {
  2708. X            list[top] = list[k];
  2709. X            list[k] = temp;
  2710. X
  2711. X            /* Make the changed list a heap */
  2712. X
  2713. X            adjust(list, k, bottom);    /* recursive */
  2714. X        }
  2715. X    }
  2716. X}
  2717. X
  2718. X/*
  2719. X * Compare two trees, if a > b return true, else return false. Note
  2720. X * comparison rules in previous comments.
  2721. X */
  2722. X
  2723. static int
  2724. cmptrees(a, b)
  2725. X    int        a, b;    /* root nodes of trees */
  2726. X{
  2727. X    if (node[a].weight > node[b].weight)
  2728. X        return TRUE;
  2729. X    if (node[a].weight == node[b].weight)
  2730. X        if (node[a].tdepth > node[b].tdepth)
  2731. X            return TRUE;
  2732. X    return FALSE;
  2733. X}
  2734. X
  2735. X/*
  2736. X * HUFFMAN ALGORITHM: develops the single element trees into a single binary
  2737. X * tree by forming subtrees rooted in interior nodes having weights equal to
  2738. X * the sum of weights of all their descendents and having depth counts
  2739. X * indicating the depth of their longest paths.
  2740. X *
  2741. X * When all trees have been formed into a single tree satisfying the heap
  2742. X * property (on weight, with depth as a tie breaker) then the binary code
  2743. X * assigned to a leaf (value to be encoded) is then the series of left (0)
  2744. X * and right (1) paths leading from the root to the leaf. Note that trees are
  2745. X * removed from the heaped list by moving the last element over the top
  2746. X * element and reheaping the shorter list.
  2747. X */
  2748. X
  2749. X#define    MAXCHAR(a,b)    ((a > b) ? a : b)
  2750. X
  2751. static VOID
  2752. bld_tree(list, len)
  2753. X    int        list[];
  2754. X    int        len;
  2755. X{
  2756. X    register int    freenode;    /* next free node in tree */
  2757. X    register struct nd *frnp;    /* free node pointer */
  2758. X    int        lch, rch;    /* temps for left, right children */
  2759. X
  2760. X    /*
  2761. X     * Initialize index to next available (non-leaf) node. Lower numbered
  2762. X     * nodes correspond to leaves (data values).
  2763. X     */
  2764. X
  2765. X    freenode = NUMVALS;
  2766. X
  2767. X    while (len > 1) {    /* Take from list two btrees with least
  2768. X                 * weight and build an interior node pointing
  2769. X                 * to them. This forms a new tree. */
  2770. X
  2771. X        lch = list[0];    /* This one will be left child */
  2772. X
  2773. X        /* delete top (least) tree from the list of trees */
  2774. X
  2775. X        list[0] = list[--len];
  2776. X        adjust(list, 0, len - 1);
  2777. X
  2778. X        /* Take new top (least) tree. Reuse list slot later */
  2779. X
  2780. X        rch = list[0];    /* This one will be right child */
  2781. X
  2782. X        /*
  2783. X         * Form new tree from the two least trees using a free node
  2784. X         * as root. Put the new tree in the list.
  2785. X         */
  2786. X
  2787. X        frnp = &node[freenode]; /* address of next free node */
  2788. X        list[0] = freenode++;    /* put at top for now */
  2789. X        frnp->lchild = lch;
  2790. X        frnp->rchild = rch;
  2791. X        frnp->weight = node[lch].weight + node[rch].weight;
  2792. X        frnp->tdepth = 1 + MAXCHAR(node[lch].tdepth, node[rch].tdepth);
  2793. X
  2794. X        /* reheap list    to get least tree at top */
  2795. X
  2796. X        adjust(list, 0, len - 1);
  2797. X    }
  2798. X    dctreehd = list[0];    /* head of final tree */
  2799. X}
  2800. X
  2801. static VOID
  2802. init_enc()
  2803. X{
  2804. X    register int    i;
  2805. X
  2806. X    /* Initialize encoding table */
  2807. X
  2808. X    for (i = 0; i < NUMVALS; ++i)
  2809. X        codelen[i] = 0;
  2810. X}
  2811. X
  2812. X/*
  2813. X * Recursive routine to walk the indicated subtree and level and maintain the
  2814. X * current path code in bstree. When a leaf is found the entire code string
  2815. X * and length are put into the encoding table entry for the leaf's data value.
  2816. X *
  2817. X * Returns ERROR if codes are too long.
  2818. X */
  2819. X
  2820. static int
  2821. buildenc(level, root)
  2822. X    int        level;    /* level of tree being examined, from zero */
  2823. X    int        root;    /* root of subtree is also data value if leaf */
  2824. X{
  2825. X    register int    l, r;
  2826. X
  2827. X    l = node[root].lchild;
  2828. X    r = node[root].rchild;
  2829. X
  2830. X    if (l == NOCHILD && r == NOCHILD) {    /* Leaf. Previous path
  2831. X                         * determines bit string code
  2832. X                         * of length level (bits 0 to
  2833. X                         * level - 1). Ensures unused
  2834. X                         * code bits are zero. */
  2835. X
  2836. X        codelen[root] = level;
  2837. X        code[root] = tcode & (((u_short) ~ 0) >> (16 - level));
  2838. X        return (level > 16) ? ERROR : 0;
  2839. X    } else {
  2840. X        if (l != NOCHILD) {    /* Clear path bit and continue deeper */
  2841. X
  2842. X            tcode &= ~(1 << level);
  2843. X            if (buildenc(level + 1, l) == ERROR)
  2844. X                return ERROR;    /* pass back bad statuses */
  2845. X        }
  2846. X        if (r != NOCHILD) {    /* Set path bit and continue deeper */
  2847. X
  2848. X            tcode |= 1 << level;
  2849. X            if (buildenc(level + 1, r) == ERROR)
  2850. X                return ERROR;    /* pass back bad statuses */
  2851. X        }
  2852. X    }
  2853. X    return 0;        /* it worked if we reach here */
  2854. X}
  2855. X
  2856. X#define OUT_INT(n) \
  2857. X    *outbeg++ = n & 0xff;    /* first the low byte */ \
  2858. X    *outbeg++ = n >> 8;    /* then the high byte */
  2859. X
  2860. X/* Write out the header of the compressed file */
  2861. X
  2862. long
  2863. head_sq()
  2864. X{
  2865. X    register int    l, r;
  2866. X    int        i, k;
  2867. X    int        numnodes;    /* # of nodes in simplified tree */
  2868. X
  2869. X    outbeg = outbuf;
  2870. X
  2871. X    /*
  2872. X     * Write out a simplified decoding tree. Only the interior nodes are
  2873. X     * written. When a child is a leaf index (representing a data value)
  2874. X     * it is recoded as -(index + 1) to distinguish it from interior
  2875. X     * indexes which are recoded as positive indexes in the new tree.
  2876. X     * 
  2877. X     * Note that this tree will be empty for an empty file.
  2878. X     */
  2879. X
  2880. X    numnodes = dctreehd < NUMVALS ? 0 : dctreehd - (NUMVALS - 1);
  2881. X    OUT_INT(numnodes)
  2882. X        for (k = 0, i = dctreehd; k < numnodes; ++k, --i) {
  2883. X        l = node[i].lchild;
  2884. X        r = node[i].rchild;
  2885. X        l = l < NUMVALS ? -(l + 1) : dctreehd - l;
  2886. X        r = r < NUMVALS ? -(r + 1) : dctreehd - r;
  2887. X        OUT_INT(l)
  2888. X            OUT_INT(r)
  2889. X    }
  2890. X
  2891. X    return sizeof(short) + numnodes * 2 * sizeof(short);
  2892. X}
  2893. X
  2894. X/*
  2895. X * This routine is used to perform the actual squeeze operation.  It can only
  2896. X * be called after the file has been scanned.  It returns the true length of
  2897. X * the squeezed entry.
  2898. X *
  2899. X * There are two unsynchronized bit-byte relationships here. The input stream
  2900. X * bytes are converted to bit strings of various lengths via the static
  2901. X * variables named c... These bit strings are concatenated without padding to
  2902. X * become the stream of encoded result bytes, which this function returns one
  2903. X * at a time. The EOF (end of file) is converted to SPEOF for convenience and
  2904. X * encoded like any other input value. True EOF is returned after that.
  2905. X */
  2906. X
  2907. long
  2908. huf_buf(pbuf, plen, len, ob)
  2909. X    u_char           *pbuf;    /* ncr'd input buffer */
  2910. X    u_int        plen;    /* length of pack buffer */
  2911. X    u_int        len;    /* length of input buffer */
  2912. X    FILE           *ob;    /* output file */
  2913. X{
  2914. X    int        rbyte;    /* Result byte value */
  2915. X    int        need;    /* number of bits */
  2916. X    long        size = 0;
  2917. X
  2918. X    if (len == 0)        /* Account for EOF/SPEOF */
  2919. X        plen++;
  2920. X
  2921. X    while (plen != 0) {
  2922. X        if (outbeg > outend) {
  2923. X            putb_pak(outbuf, (u_int) (outbeg-outbuf), ob);
  2924. X            outbeg = outbuf;
  2925. X        }
  2926. X        rbyte = 0;
  2927. X        need = 8;    /* build one byte per call */
  2928. X
  2929. X        /*
  2930. X         * Loop to build a byte of encoded data.
  2931. X         */
  2932. X
  2933. loop:
  2934. X        if (cbitsrem >= need) { /* if current code is big enough */
  2935. X            if (need == 0) {
  2936. X                *outbeg++ = rbyte;
  2937. X                size++;
  2938. X                continue;
  2939. X            }
  2940. X            rbyte |= ccode << (8 - need);    /* take what we need */
  2941. X            ccode >>= need; /* and leave the rest */
  2942. X            cbitsrem -= need;
  2943. X            *outbeg++ = rbyte & 0xff;
  2944. X            size++;
  2945. X            continue;
  2946. X        }
  2947. X        /* We need more than current code */
  2948. X
  2949. X        if (cbitsrem > 0) {
  2950. X            rbyte |= ccode << (8 - need);    /* take what there is */
  2951. X            need -= cbitsrem;
  2952. X        }
  2953. X        /* No more bits in current code string */
  2954. X
  2955. X        if (curin == SPEOF) {    /* The end of file token has been
  2956. X                     * encoded. Save result byte. */
  2957. X            cbitsrem = 0;
  2958. X            if (need != 8) {
  2959. X                *outbeg++ = rbyte;
  2960. X                size++;
  2961. X            }
  2962. X            break;
  2963. X        }
  2964. X        /* Get an input byte */
  2965. X
  2966. X        if (plen == 1 && len == 0)
  2967. X            curin = SPEOF;    /* convenient for encoding */
  2968. X        else
  2969. X            curin = *pbuf++;
  2970. X        plen--;
  2971. X
  2972. X        ccode = code[curin];    /* get the new byte's code */
  2973. X        cbitsrem = codelen[curin];
  2974. X
  2975. X        goto loop;
  2976. X    }
  2977. X    if (len == 0)
  2978. X        putb_pak(outbuf, (u_int) (outbeg - outbuf), ob);
  2979. X    return (size);
  2980. X}
  2981. END_OF_FILE
  2982. if test 13957 -ne `wc -c <'arcsq.c'`; then
  2983.     echo shar: \"'arcsq.c'\" unpacked with wrong size!
  2984. fi
  2985. # end of 'arcsq.c'
  2986. fi
  2987. if test -f 'marc.c' -a "${1}" != "-c" ; then 
  2988.   echo shar: Will not clobber existing file \"'marc.c'\"
  2989. else
  2990. echo shar: Extracting \"'marc.c'\" \(9470 characters\)
  2991. sed "s/^X//" >'marc.c' <<'END_OF_FILE'
  2992. X/*
  2993. X * $Header: /var/local/hyc/src/arc/RCS/marc.c,v 2.0 1991/11/12 00:39:44 hyc Exp $
  2994. X */
  2995. X
  2996. X/*  MARC - Archive merge utility
  2997. X
  2998. X    Version 5.21, created on 04/22/87 at 15:05:10
  2999. X
  3000. X(C) COPYRIGHT 1985-87 by System Enhancement Associates; ALL RIGHTS RESERVED
  3001. X
  3002. X    By:     Thom Henderson
  3003. X
  3004. X    Description:
  3005. X     This program is used to "merge" archives.  That is, to move
  3006. X     files from one archive to another with no data conversion.
  3007. X     Please refer to the ARC source for a description of archives
  3008. X     and archive formats.
  3009. X
  3010. X    Instructions:
  3011. X     Run this program with no arguments for complete instructions.
  3012. X
  3013. X    Language:
  3014. X     Computer Innovations Optimizing C86
  3015. X*/
  3016. X#include <stdio.h>
  3017. X#include "arc.h"
  3018. X
  3019. X#if    UNIX
  3020. X#include <sys/types.h>
  3021. X#include <sys/stat.h>
  3022. X#endif
  3023. X
  3024. X#ifndef    __STDC__
  3025. char *calloc(), *malloc(), *realloc(); /* memory managers */
  3026. X#endif
  3027. VOID    arcdie();
  3028. static VOID expandlst(), merge();
  3029. X
  3030. XFILE *src;                   /* source archive */
  3031. char srcname[STRLEN];               /* source archive name */
  3032. X
  3033. static char **lst;               /* files list */
  3034. static int lnum;               /* length of files list */
  3035. X
  3036. int
  3037. main(nargs,arg)                   /* system entry point */
  3038. int nargs;                   /* number of arguments */
  3039. char *arg[];                   /* pointers to arguments */
  3040. X{
  3041. X    char *makefnam();               /* filename fixup routine */
  3042. X    char *envfind();
  3043. X#if    !_MTS
  3044. X    char *arctemp2, *mktemp();        /* temp file stuff */
  3045. X#endif
  3046. X#if    GEMDOS
  3047. X    VOID exitpause();
  3048. X#endif
  3049. X    int n;                   /* index */
  3050. X#if    UNIX
  3051. X    struct    stat    sbuf;
  3052. X#endif
  3053. X
  3054. X
  3055. X    if(nargs<3)
  3056. X    {     printf("MARC - Archive merger, Version 5.21, created on 04/22/87 at 15:05:10\n");
  3057. X/*     printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  3058. X     printf(" ALL RIGHTS RESERVED\n\n");
  3059. X     printf("Please refer all inquiries to:\n\n");
  3060. X     printf("    System Enhancement Associates\n");
  3061. X     printf("    21 New Street, Wayne NJ 07470\n\n");
  3062. X     printf("You may copy and distribute this program freely,");
  3063. X     printf(" provided that:\n");
  3064. X     printf("    1)      No fee is charged for such copying and");
  3065. X     printf(" distribution, and\n");
  3066. X     printf("    2)      It is distributed ONLY in its original,");
  3067. X     printf(" unmodified state.\n\n");
  3068. X     printf("If you like this program, and find it of use, then your");
  3069. X     printf(" contribution will\n");
  3070. X     printf("be appreciated.  You may not use this product in a");
  3071. X     printf(" commercial environment\n");
  3072. X     printf("or a governmental organization without paying a license");
  3073. X     printf(" fee of $35.  Site\n");
  3074. X     printf("licenses and commercial distribution licenses are");
  3075. X     printf(" available.  A program\n");
  3076. X     printf("disk and printed documentation are available for $50.\n");
  3077. X     printf("\nIf you fail to abide by the terms of this license, ");
  3078. X     printf(" then your conscience\n");
  3079. X     printf("will haunt you for the rest of your life.\n\n");
  3080. X*/
  3081. X     printf("Usage: MARC <tgtarc> <srcarc> [<filename> . . .]\n");
  3082. X     printf("Where: <tgtarc> is the archive to add files to,\n");
  3083. X     printf("    <srcarc> is the archive to get files from, and\n");
  3084. X     printf("    <filename> is zero or more file names to get.\n");
  3085. X     printf("\nAdapted from MSDOS by Howard Chu\n");
  3086. X#if    GEMDOS
  3087. X     exitpause();
  3088. X#endif
  3089. X     return 1;
  3090. X    }
  3091. X
  3092. X    /* see where temp files go */
  3093. X#if    !_MTS
  3094. X    arctemp = calloc(1, STRLEN);
  3095. X    if (!(arctemp2 = envfind("ARCTEMP")))
  3096. X        arctemp2 = envfind("TMPDIR");
  3097. X    if (arctemp2) {
  3098. X        strcpy(arctemp, arctemp2);
  3099. X        n = strlen(arctemp);
  3100. X        if (arctemp[n - 1] != CUTOFF)
  3101. X            arctemp[n] = CUTOFF;
  3102. X    }
  3103. X#if    UNIX
  3104. X    else    strcpy(arctemp, "/tmp/");
  3105. X#endif
  3106. X#if    !MSDOS
  3107. X    {
  3108. X        static char tempname[] = "AXXXXXX";
  3109. X        strcat(arctemp, mktemp(tempname));
  3110. X    }
  3111. X#else
  3112. X    strcat(arctemp, "$ARCTEMP");
  3113. X#endif
  3114. X#else
  3115. X    guinfo("SHFSEP    ", gotinf);
  3116. X    sepchr[0] = gotinf[0];
  3117. X    guinfo("SCRFCHAR", gotinf);
  3118. X    tmpchr[0] = gotinf[0];
  3119. X    arctemp = "-$$$";
  3120. X    arctemp[0] = tmpchr[0];
  3121. X#endif
  3122. X
  3123. X#if    UNIX
  3124. X    if (!stat(arg[1],&sbuf))
  3125. X        strcpy(arcname,arg[1]);
  3126. X    else
  3127. X        makefnam(arg[1],".arc",arcname);
  3128. X    if (!stat(arg[2],&sbuf))
  3129. X        strcpy(srcname,arg[2]);
  3130. X    else
  3131. X        makefnam(arg[2],".arc",srcname);
  3132. X#else
  3133. X    makefnam(arg[1],".ARC",arcname);   /* fix up archive names */
  3134. X    makefnam(arg[2],".ARC",srcname);
  3135. X/*    makefnam(".$$$",arcname,newname);*/
  3136. X#endif
  3137. X    sprintf(newname,"%s.arc",arctemp);
  3138. X
  3139. X    arc = fopen(arcname,OPEN_R);           /* open the archives */
  3140. X    if(!(src=fopen(srcname,OPEN_R)))
  3141. X     arcdie("Cannot read source archive %s",srcname);
  3142. X    if(!(new=fopen(newname,OPEN_W)))
  3143. X     arcdie("Cannot create new archive %s",newname);
  3144. X
  3145. X    if(!arc)
  3146. X     printf("Creating new archive %s\n",arcname);
  3147. X
  3148. X    /* get the files list set up */
  3149. X
  3150. X    lnum = nargs-3;               /* initial length of list */
  3151. X    if(lnum<1)                   /* phoney for default case */
  3152. X    {     lnum = 1;
  3153. X     lst = (char **) calloc(1,sizeof(char *));
  3154. X     lst[0] = "*.*";
  3155. X    }
  3156. X    else                   /* else use filenames given */
  3157. X    {     lst = (char **) calloc(lnum,sizeof(char *));
  3158. X     for(n=3; n<nargs; n++)
  3159. X          lst[n-3] = arg[n];
  3160. X
  3161. X     for(n=0; n<lnum; )           /* expand indirect references */
  3162. X     {    if(*lst[n] == '@')
  3163. X           expandlst(n);
  3164. X          else n++;
  3165. X     }
  3166. X    }
  3167. X
  3168. X    merge(lnum,lst);               /* merge desired files */
  3169. X
  3170. X    if(arc) fclose(arc);           /* close the archives */
  3171. X    fclose(src);
  3172. X    fclose(new);
  3173. X
  3174. X    if(arc)                   /* make the switch */
  3175. X     if(unlink(arcname))
  3176. X          arcdie("Unable to delete old copy of %s",arcname);
  3177. X    if(move(newname,arcname))
  3178. X     arcdie("Unable to rename %s to %s",newname,arcname);
  3179. X
  3180. X    setstamp(arcname,arcdate,arctime);     /* new arc matches newest file */
  3181. X
  3182. X#if    GEMDOS
  3183. X    exitpause();
  3184. X#endif
  3185. X    return nerrs;
  3186. X}
  3187. X
  3188. static    VOID
  3189. merge(nargs,arg)               /* merge two archives */
  3190. int nargs;                   /* number of filename templates */
  3191. char *arg[];                   /* pointers to names */
  3192. X{
  3193. X    struct heads srch;               /* source archive header */
  3194. X    struct heads arch;               /* target archive header */
  3195. X    int gotsrc, gotarc;               /* archive entry versions (0=end) */
  3196. X    int copy;                   /* true to copy file from source */
  3197. X    int n;                   /* index */
  3198. X
  3199. X    gotsrc = gethdr(src,&srch);           /* get first source file */
  3200. X    gotarc = gethdr(arc,&arch);           /* get first target file */
  3201. X
  3202. X    while(gotsrc || gotarc)           /* while more to merge */
  3203. X    {     if(strcmp(srch.name,arch.name)>0)
  3204. X     {    copyfile(arc,&arch,gotarc);
  3205. X          gotarc = gethdr(arc,&arch);
  3206. X     }
  3207. X
  3208. X     else if(strcmp(srch.name,arch.name)<0)
  3209. X     {    copy = 0;
  3210. X          for(n=0; n<nargs; n++)
  3211. X          {       if(match(srch.name,arg[n]))
  3212. X           {    copy = 1;
  3213. X            break;
  3214. X           }
  3215. X          }
  3216. X          if(copy)               /* select source or target */
  3217. X          {       printf("Adding file:      %s\n",srch.name);
  3218. X           copyfile(src,&srch,gotsrc);
  3219. X          }
  3220. X          else fseek(src,srch.size,1);
  3221. X          gotsrc = gethdr(src,&srch);
  3222. X     }
  3223. X
  3224. X     else                   /* duplicate names */
  3225. X     {    copy = 0;
  3226. X          {       if((srch.date>arch.date)
  3227. X           || (srch.date==arch.date && srch.time>arch.time))
  3228. X           {    for(n=0; n<nargs; n++)
  3229. X            {    if(match(srch.name,arg[n]))
  3230. X                 {      copy = 1;
  3231. X                  break;
  3232. X                 }
  3233. X            }
  3234. X           }
  3235. X          }
  3236. X          if(copy)               /* select source or target */
  3237. X          {       printf("Updating file: %s\n",srch.name);
  3238. X           copyfile(src,&srch,gotsrc);
  3239. X           gotsrc = gethdr(src,&srch);
  3240. X           if(gotarc)
  3241. X           {    fseek(arc,arch.size,1);
  3242. X            gotarc = gethdr(arc,&arch);
  3243. X           }
  3244. X          }
  3245. X          else
  3246. X          {       copyfile(arc,&arch,gotarc);
  3247. X           gotarc = gethdr(arc,&arch);
  3248. X           if(gotsrc)
  3249. X           {    fseek(src,srch.size,1);
  3250. X            gotsrc = gethdr(src,&srch);
  3251. X           }
  3252. X          }
  3253. X     }
  3254. X    }
  3255. X
  3256. X    hdrver = 0;                   /* end of archive marker */
  3257. X    writehdr(&arch,new);           /* mark the end of the archive */
  3258. X}
  3259. X
  3260. int gethdr(f,hdr)               /* special read header for merge */
  3261. XFILE *f;                   /* file to read from */
  3262. struct heads *hdr;               /* storage for header */
  3263. X{
  3264. X    char *i = hdr->name;           /* string index */
  3265. X    int n;                   /* index */
  3266. X
  3267. X    for(n=0; n<FNLEN; n++)           /* fill name field */
  3268. X     *i++ = 0176;               /* impossible high value */
  3269. X    *--i = '\0';               /* properly end the name */
  3270. X
  3271. X    hdrver = 0;                   /* reset header version */
  3272. X    if(readhdr(hdr,f))               /* use normal reading logic */
  3273. X     return hdrver;               /* return the version */
  3274. X    else return 0;               /* or fake end of archive */
  3275. X}
  3276. X
  3277. copyfile(f,hdr,ver)               /* copy a file from an archive */
  3278. XFILE *f;                   /* archive to copy from */
  3279. struct heads *hdr;               /* header data for file */
  3280. int ver;                   /* header version */
  3281. X{
  3282. X    hdrver = ver;               /* set header version */
  3283. X    writehdr(hdr,new);               /* write out the header */
  3284. X    filecopy(f,new,hdr->size);           /* copy over the data */
  3285. X}
  3286. X
  3287. static VOID
  3288. expandlst(n)                   /* expand an indirect reference */
  3289. int n;                       /* number of entry to expand */
  3290. X{
  3291. X    FILE *lf, *fopen();               /* list file, opener */
  3292. X    char buf[100];               /* input buffer */
  3293. X    int x;                   /* index */
  3294. X    char *p = lst[n]+1;               /* filename pointer */
  3295. X
  3296. X    if(*p)                   /* use name if one was given */
  3297. X    {     makefnam(p,".CMD",buf);
  3298. X     upper(buf);
  3299. X     if(!(lf=fopen(buf,"r")))
  3300. X          arcdie("Cannot read list of files in %s",buf);
  3301. X    }
  3302. X    else lf = stdin;               /* else use standard input */
  3303. X
  3304. X    for(x=n+1; x<lnum; x++)           /* drop reference from the list */
  3305. X     lst[x-1] = lst[x];
  3306. X    lnum--;
  3307. X
  3308. X    while(fscanf(lf,"%99s",buf)>0)     /* read in the list */
  3309. X    {     if(!(lst=(char **) realloc(lst,(lnum+1)*sizeof(char *))))
  3310. X          arcdie("too many file references");
  3311. X
  3312. X     lst[lnum] = malloc(strlen(buf)+1);
  3313. X     strcpy(lst[lnum],buf);           /* save the name */
  3314. X     lnum++;
  3315. X    }
  3316. X
  3317. X    if(lf!=stdin)               /* avoid closing standard input */
  3318. X     fclose(lf);
  3319. X}
  3320. END_OF_FILE
  3321. if test 9470 -ne `wc -c <'marc.c'`; then
  3322.     echo shar: \"'marc.c'\" unpacked with wrong size!
  3323. fi
  3324. # end of 'marc.c'
  3325. fi
  3326. echo shar: End of archive 2 \(of 3\).
  3327. cp /dev/null ark2isdone
  3328. MISSING=""
  3329. for I in 1 2 3 ; do
  3330.     if test ! -f ark${I}isdone ; then
  3331.     MISSING="${MISSING} ${I}"
  3332.     fi
  3333. done
  3334. if test "${MISSING}" = "" ; then
  3335.     echo You have unpacked all 3 archives.
  3336.     rm -f ark[1-9]isdone
  3337. else
  3338.     echo You still need to unpack the following archives:
  3339.     echo "        " ${MISSING}
  3340. fi
  3341. ##  End of shell archive.
  3342. exit 0
  3343.